1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2024 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "Compiler/Optimizer/OpenCLPasses/PrivateMemory/LowerByValAttribute.hpp"
#include "Compiler/IGCPassSupport.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/Pass.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include <llvmWrapper/IR/Instructions.h>
#include <llvmWrapper/IR/IRBuilder.h>
#include "common/LLVMWarningsPop.hpp"
using namespace llvm;
using namespace IGC;
char LowerByValAttribute::ID = 0;
// Register pass to igc-opt
#define PASS_FLAG "igc-lower-byval-attribute"
#define PASS_DESCRIPTION "LowerByValAttribute"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(LowerByValAttribute, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_END(LowerByValAttribute, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
// This pass lowers usage of `byval` attribute into an explicit copy (alloca + memcpy).
//
// According to the LLVM documentation:
// "The attribute implies that a hidden copy of the pointee is made between the caller
// and the callee, so the callee is unable to modify the value in the caller."
//
// The "hidden copy" means that frontend compilers don't generate an explicit copy in SPIRV,
// but just decorates a pointer with ByVal. It moves the responsibility, for generating a copy,
// to the backend compiler (IGC).
LowerByValAttribute::LowerByValAttribute(void) : FunctionPass(ID) {
initializeLowerByValAttributePass(*PassRegistry::getPassRegistry());
}
bool LowerByValAttribute::runOnFunction(Function &F) {
visit(F);
return m_changed;
}
void LowerByValAttribute::visitCallInst(CallInst &CI) {
auto &DL = CI.getModule()->getDataLayout();
// Skip intrinsics
auto F = CI.getCalledFunction();
if (F && F->isDeclaration())
return;
for (unsigned i = 0; i < IGCLLVM::getNumArgOperands(&CI); ++i) {
Value *OpI = CI.getArgOperand(i);
if (isa<UndefValue>(OpI))
continue;
Type *OpITy = OpI->getType();
if (!OpITy->isPointerTy())
continue;
if (CI.paramHasAttr(i, llvm::Attribute::ByVal) && !CI.paramHasAttr(i, llvm::Attribute::ReadOnly)) {
Type *ElTy = CI.getParamByValType(i);
IGCLLVM::IRBuilder<> builder(&CI);
Value *AI = builder.CreateAlloca(ElTy);
builder.CreateMemCpy(AI, OpI, DL.getTypeAllocSize(ElTy), DL.getABITypeAlign(ElTy).value());
auto AC = builder.CreateAddrSpaceCast(AI, OpITy);
CI.replaceUsesOfWith(OpI, AC);
m_changed = true;
}
}
}
|