Skip to content

Commit

Permalink
[CodeGen] Avoid pointer element type access for blocks
Browse files Browse the repository at this point in the history
Pass the block struct type down to the TargetInfo hooks.
  • Loading branch information
nikic committed Mar 17, 2022
1 parent 33d7417 commit 6e1e99d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
5 changes: 3 additions & 2 deletions clang/lib/CodeGen/CGBlocks.cpp
Expand Up @@ -1105,7 +1105,7 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {

if (IsOpenCL) {
CGM.getOpenCLRuntime().recordBlockInfo(blockInfo.BlockExpression, InvokeFn,
result);
result, blockInfo.StructureType);
}

return result;
Expand Down Expand Up @@ -1401,7 +1401,8 @@ static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
if (CGM.getContext().getLangOpts().OpenCL)
CGM.getOpenCLRuntime().recordBlockInfo(
blockInfo.BlockExpression,
cast<llvm::Function>(blockFn->stripPointerCasts()), Result);
cast<llvm::Function>(blockFn->stripPointerCasts()), Result,
literal->getValueType());
return Result;
}

Expand Down
6 changes: 3 additions & 3 deletions clang/lib/CodeGen/CGOpenCLRuntime.cpp
Expand Up @@ -148,13 +148,14 @@ static const BlockExpr *getBlockExpr(const Expr *E) {
/// corresponding block expression.
void CGOpenCLRuntime::recordBlockInfo(const BlockExpr *E,
llvm::Function *InvokeF,
llvm::Value *Block) {
llvm::Value *Block, llvm::Type *BlockTy) {
assert(EnqueuedBlockMap.find(E) == EnqueuedBlockMap.end() &&
"Block expression emitted twice");
assert(isa<llvm::Function>(InvokeF) && "Invalid invoke function");
assert(Block->getType()->isPointerTy() && "Invalid block literal type");
EnqueuedBlockMap[E].InvokeFunc = InvokeF;
EnqueuedBlockMap[E].BlockArg = Block;
EnqueuedBlockMap[E].BlockTy = BlockTy;
EnqueuedBlockMap[E].Kernel = nullptr;
}

Expand All @@ -179,8 +180,7 @@ CGOpenCLRuntime::emitOpenCLEnqueuedBlock(CodeGenFunction &CGF, const Expr *E) {
}

auto *F = CGF.getTargetHooks().createEnqueuedBlockKernel(
CGF, EnqueuedBlockMap[Block].InvokeFunc,
EnqueuedBlockMap[Block].BlockArg->stripPointerCasts());
CGF, EnqueuedBlockMap[Block].InvokeFunc, EnqueuedBlockMap[Block].BlockTy);

// The common part of the post-processing of the kernel goes here.
F->addFnAttr(llvm::Attribute::NoUnwind);
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/CodeGen/CGOpenCLRuntime.h
Expand Up @@ -46,6 +46,7 @@ class CGOpenCLRuntime {
llvm::Function *InvokeFunc; /// Block invoke function.
llvm::Function *Kernel; /// Enqueued block kernel.
llvm::Value *BlockArg; /// The first argument to enqueued block kernel.
llvm::Type *BlockTy; /// Type of the block argument.
};
/// Maps block expression to block information.
llvm::DenseMap<const Expr *, EnqueuedBlockInfo> EnqueuedBlockMap;
Expand Down Expand Up @@ -93,7 +94,7 @@ class CGOpenCLRuntime {
/// \param InvokeF invoke function emitted for the block expression.
/// \param Block block literal emitted for the block expression.
void recordBlockInfo(const BlockExpr *E, llvm::Function *InvokeF,
llvm::Value *Block);
llvm::Value *Block, llvm::Type *BlockTy);

/// \return LLVM block invoke function emitted for an expression derived from
/// the block expression.
Expand Down
7 changes: 3 additions & 4 deletions clang/lib/CodeGen/TargetInfo.cpp
Expand Up @@ -9214,7 +9214,7 @@ class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
llvm::Function *
createEnqueuedBlockKernel(CodeGenFunction &CGF,
llvm::Function *BlockInvokeFunc,
llvm::Value *BlockLiteral) const override;
llvm::Type *BlockTy) const override;
bool shouldEmitStaticExternCAliases() const override;
void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
};
Expand Down Expand Up @@ -11461,7 +11461,7 @@ const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
llvm::Function *
TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
llvm::Function *Invoke,
llvm::Value *BlockLiteral) const {
llvm::Type *BlockTy) const {
auto *InvokeFT = Invoke->getFunctionType();
llvm::SmallVector<llvm::Type *, 2> ArgTys;
for (auto &P : InvokeFT->params())
Expand Down Expand Up @@ -11495,11 +11495,10 @@ TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
/// has "enqueued-block" function attribute and kernel argument metadata.
llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
CodeGenFunction &CGF, llvm::Function *Invoke,
llvm::Value *BlockLiteral) const {
llvm::Type *BlockTy) const {
auto &Builder = CGF.Builder;
auto &C = CGF.getLLVMContext();

auto *BlockTy = BlockLiteral->getType()->getPointerElementType();
auto *InvokeFT = Invoke->getFunctionType();
llvm::SmallVector<llvm::Type *, 2> ArgTys;
llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/TargetInfo.h
Expand Up @@ -329,7 +329,7 @@ class TargetCodeGenInfo {
virtual llvm::Function *
createEnqueuedBlockKernel(CodeGenFunction &CGF,
llvm::Function *BlockInvokeFunc,
llvm::Value *BlockLiteral) const;
llvm::Type *BlockTy) const;

/// \return true if the target supports alias from the unmangled name to the
/// mangled name of functions declared within an extern "C" region and marked
Expand Down

0 comments on commit 6e1e99d

Please sign in to comment.