Skip to content

AMDGPU: Move enqueued block handling into clang #128519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions clang/lib/CodeGen/Targets/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,20 @@ void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention(
FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
}

/// Return IR struct type for rtinfo struct in rocm-device-libs used for device
/// enqueue.
///
/// ptr addrspace(1) kernel_object, i32 private_segment_size,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this from the implicit arguments? Could that be considered constant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this isn't in the implicit arguments. It could be constant, but I think I had difficulty getting externally_initialized + constant to work as expected.

I also think in principle the special pass isn't necessary anymore. I think something about visibility was how it ended up this way, and might require a runtime change to fully delete it

/// i32 group_segment_size

static llvm::StructType *
getAMDGPURuntimeHandleType(llvm::LLVMContext &C,
llvm::Type *KernelDescriptorPtrTy) {
llvm::Type *Int32 = llvm::Type::getInt32Ty(C);
return llvm::StructType::create(C, {KernelDescriptorPtrTy, Int32, Int32},
"block.runtime.handle.t");
}

/// Create an OpenCL kernel for an enqueued block.
///
/// The type of the first argument (the block literal) is the struct type
Expand Down Expand Up @@ -653,23 +667,29 @@ llvm::Value *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
ArgNames.push_back(
llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
}
std::string Name = Invoke->getName().str() + "_kernel";

llvm::Module &Mod = CGF.CGM.getModule();
const llvm::DataLayout &DL = Mod.getDataLayout();

llvm::Twine Name = Invoke->getName() + "_kernel";
auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);

// The kernel itself can be internal, the runtime does not directly access the
// kernel address (only the kernel descriptor).
auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
&CGF.CGM.getModule());
&Mod);
F->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL);

llvm::AttrBuilder KernelAttrs(C);
// FIXME: The invoke isn't applying the right attributes either
// FIXME: This is missing setTargetAttributes
CGF.CGM.addDefaultFunctionDefinitionAttributes(KernelAttrs);
KernelAttrs.addAttribute("enqueued-block");
F->addFnAttrs(KernelAttrs);

auto IP = CGF.Builder.saveIP();
auto *BB = llvm::BasicBlock::Create(C, "entry", F);
Builder.SetInsertPoint(BB);
const auto BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(BlockTy);
const auto BlockAlign = DL.getPrefTypeAlign(BlockTy);
auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
BlockPtr->setAlignment(BlockAlign);
Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
Expand All @@ -692,7 +712,39 @@ llvm::Value *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));

return F;
llvm::StructType *HandleTy = getAMDGPURuntimeHandleType(
C, llvm::PointerType::get(C, DL.getDefaultGlobalsAddressSpace()));
llvm::Constant *RuntimeHandleInitializer =
llvm::ConstantAggregateZero::get(HandleTy);

llvm::Twine RuntimeHandleName = F->getName() + ".runtime.handle";

// The runtime needs access to the runtime handle as an external symbol. The
// runtime handle will need to be made external later, in
// AMDGPUExportOpenCLEnqueuedBlocks. The kernel itself has a hidden reference
// inside the runtime handle, and is not directly referenced.

// TODO: We would initialize the first field by declaring F->getName() + ".kd"
// to reference the kernel descriptor. The runtime wouldn't need to bother
// setting it. We would need to have a final symbol name though.
// TODO: Can we directly use an external symbol with getGlobalIdentifier?
auto *RuntimeHandle = new llvm::GlobalVariable(
Mod, HandleTy,
/*isConstant=*/true, llvm::GlobalValue::InternalLinkage,
/*Initializer=*/RuntimeHandleInitializer, RuntimeHandleName,
/*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal,
DL.getDefaultGlobalsAddressSpace(),
/*isExternallyInitialized=*/true);

llvm::MDNode *HandleAsMD =
llvm::MDNode::get(C, llvm::ValueAsMetadata::get(RuntimeHandle));
F->setMetadata(llvm::LLVMContext::MD_associated, HandleAsMD);

RuntimeHandle->setSection(".amdgpu.kernel.runtime.handle");

CGF.CGM.addUsedGlobal(F);
CGF.CGM.addUsedGlobal(RuntimeHandle);
return RuntimeHandle;
}

void CodeGenModule::handleAMDGPUFlatWorkGroupSizeAttr(
Expand Down
1 change: 1 addition & 0 deletions clang/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ if( NOT CLANG_BUILT_STANDALONE )
llvm-dis
llvm-dwarfdump
llvm-ifs
llvm-link
llvm-lto2
llvm-mc
llvm-modextract
Expand Down
80 changes: 80 additions & 0 deletions clang/test/CodeGenOpenCL/amdgpu-enqueue-kernel-linking.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Make sure that invoking blocks in static functions with the same name in
// different modules are linked together.

// RUN: %clang_cc1 -O0 -cl-std=CL2.0 -triple amdgcn-amd-amdhsa -fno-ident -DKERNEL_NAME=test_kernel_first -DTYPE=float -DCONST=256.0f -emit-llvm-bc -o %t.0.bc %s
// RUN: %clang_cc1 -O0 -cl-std=CL2.0 -triple amdgcn-amd-amdhsa -fno-ident -DKERNEL_NAME=test_kernel_second -DTYPE=int -DCONST=128.0f -emit-llvm-bc -o %t.1.bc %s

// Make sure nothing strange happens with the linkage choices.
// RUN: opt -passes=globalopt -o %t.opt.0.bc %t.0.bc
// RUN: opt -passes=globalopt -o %t.opt.1.bc %t.1.bc

// Check the result of linking
// RUN: llvm-link -S %t.opt.0.bc %t.opt.1.bc -o - | FileCheck %s

// Make sure that a block invoke used with the same name works in multiple
// translation units

// CHECK: @llvm.used = appending addrspace(1) global [4 x ptr] [ptr @__static_invoker_block_invoke_kernel, ptr addrspacecast (ptr addrspace(1) @__static_invoker_block_invoke_kernel.runtime.handle to ptr), ptr @__static_invoker_block_invoke_kernel.2, ptr addrspacecast (ptr addrspace(1) @__static_invoker_block_invoke_kernel.runtime.handle.3 to ptr)], section "llvm.metadata"


// CHECK: @__static_invoker_block_invoke_kernel.runtime.handle = internal addrspace(1) externally_initialized constant %block.runtime.handle.t zeroinitializer, section ".amdgpu.kernel.runtime.handle"
// CHECK: @__static_invoker_block_invoke_kernel.runtime.handle.3 = internal addrspace(1) externally_initialized constant %block.runtime.handle.t zeroinitializer, section ".amdgpu.kernel.runtime.handle"

// CHECK: define internal amdgpu_kernel void @__static_invoker_block_invoke_kernel(<{ i32, i32, ptr, ptr addrspace(1), ptr addrspace(1) }> %0) #{{[0-9]+}} !associated ![[ASSOC_FIRST_MD:[0-9]+]]


// CHECK-LABEL: define internal void @__static_invoker_block_invoke(ptr noundef %.block_descriptor)
// CHECK: call float @llvm.fmuladd.f32


// CHECK-LABEL: define dso_local amdgpu_kernel void @test_kernel_first(


// CHECK-LABEL: define internal fastcc void @static_invoker(ptr addrspace(1) noundef %outptr, ptr addrspace(1) noundef %argptr)
// CHECK: call i32 @__enqueue_kernel_basic(ptr addrspace(1) %{{[0-9]+}}, i32 %{{[0-9]+}}, ptr addrspace(5) %tmp, ptr addrspacecast (ptr addrspace(1) @__static_invoker_block_invoke_kernel.runtime.handle to ptr), ptr %{{.+}})

// CHECK: declare i32 @__enqueue_kernel_basic(ptr addrspace(1), i32, ptr addrspace(5), ptr, ptr) local_unnamed_addr


// CHECK: define internal amdgpu_kernel void @__static_invoker_block_invoke_kernel.2(<{ i32, i32, ptr, ptr addrspace(1), ptr addrspace(1) }> %0) #{{[0-9]+}} !associated ![[ASSOC_SECOND_MD:[0-9]+]]
// CHECK: call void @__static_invoker_block_invoke.4(ptr %


// CHECK-LABEL: define internal void @__static_invoker_block_invoke.4(ptr noundef %.block_descriptor)
// CHECK: mul nsw i32
// CHECK: sitofp
// CHECK: fadd
// CHECK: fptosi

// CHECK-LABEL: define dso_local amdgpu_kernel void @test_kernel_second(ptr addrspace(1) noundef align 4 %outptr, ptr addrspace(1) noundef align 4 %argptr, ptr addrspace(1) noundef align 4 %difference)

// CHECK-LABEL: define internal fastcc void @static_invoker.5(ptr addrspace(1) noundef %outptr, ptr addrspace(1) noundef %argptr) unnamed_addr #{{[0-9]+}} {
// CHECK: call i32 @__enqueue_kernel_basic(ptr addrspace(1) %{{[0-9]+}}, i32 %{{[0-9]+}}, ptr addrspace(5) %tmp, ptr addrspacecast (ptr addrspace(1) @__static_invoker_block_invoke_kernel.runtime.handle.3 to ptr), ptr %{{.+}})


typedef struct {int a;} ndrange_t;

static void static_invoker(global TYPE* outptr, global TYPE* argptr) {
queue_t default_queue;
unsigned flags = 0;
ndrange_t ndrange;

enqueue_kernel(default_queue, flags, ndrange,
^(void) {
global TYPE* f = argptr;
outptr[0] = f[1] * f[2] + CONST;
});
}

kernel void KERNEL_NAME(global TYPE *outptr, global TYPE *argptr, global TYPE *difference) {
queue_t default_queue;
unsigned flags = 0;
ndrange_t ndrange;

static_invoker(outptr, argptr);

*difference = CONST;
}

// CHECK: ![[ASSOC_FIRST_MD]] = !{ptr addrspace(1) @__static_invoker_block_invoke_kernel.runtime.handle}
// CHECK: ![[ASSOC_SECOND_MD]] = !{ptr addrspace(1) @__static_invoker_block_invoke_kernel.runtime.handle.3}
Loading