jhuber6 updated this revision to Diff 508170. jhuber6 added a comment. Updating to simply add an entirely new attribute again. The existing `CUDAGlobal` attribute does what we want, but it's also highly coupled with the CUDA language. This made it pretty much impossible to find a way to re-use it without breaking existing functionality. The amount of code duplicated is minimal and this is required to be able to emit a callable kernel targeting NVPTX directly. I'd like to use this for my ongoing GPU `libc` project so I'd appreciate someone looking at this again.
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D140226/new/ https://reviews.llvm.org/D140226 Files: clang/include/clang/Basic/Attr.td clang/lib/CodeGen/TargetInfo.cpp clang/lib/Sema/SemaDeclAttr.cpp clang/test/CodeGen/nvptx_attributes.c clang/test/Misc/pragma-attribute-supported-attributes-list.test
Index: clang/test/Misc/pragma-attribute-supported-attributes-list.test =================================================================== --- clang/test/Misc/pragma-attribute-supported-attributes-list.test +++ clang/test/Misc/pragma-attribute-supported-attributes-list.test @@ -94,6 +94,7 @@ // CHECK-NEXT: NSConsumed (SubjectMatchRule_variable_is_parameter) // CHECK-NEXT: NSConsumesSelf (SubjectMatchRule_objc_method) // CHECK-NEXT: NSErrorDomain (SubjectMatchRule_enum) +// CHECK-NEXT: NVPTXKernel (SubjectMatchRule_function) // CHECK-NEXT: Naked (SubjectMatchRule_function) // CHECK-NEXT: NoBuiltin (SubjectMatchRule_function) // CHECK-NEXT: NoCommon (SubjectMatchRule_variable) Index: clang/test/CodeGen/nvptx_attributes.c =================================================================== --- /dev/null +++ clang/test/CodeGen/nvptx_attributes.c @@ -0,0 +1,23 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --function-signature --check-attributes --check-globals +// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -target-cpu sm_61 -emit-llvm %s -o - | FileCheck %s + +// CHECK: Function Attrs: noinline nounwind optnone +// CHECK-LABEL: define {{[^@]+}}@foo +// CHECK-SAME: (ptr noundef [[RET:%.*]]) #[[ATTR0:[0-9]+]] { +// CHECK-NEXT: entry: +// CHECK-NEXT: [[RET_ADDR:%.*]] = alloca ptr, align 8 +// CHECK-NEXT: store ptr [[RET]], ptr [[RET_ADDR]], align 8 +// CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr [[RET_ADDR]], align 8 +// CHECK-NEXT: store i32 1, ptr [[TMP0]], align 4 +// CHECK-NEXT: ret void +// +__attribute__((nvptx_kernel)) void foo(int *ret) { + *ret = 1; +} +//. +// CHECK: attributes #0 = { noinline nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="sm_61" "target-features"="+ptx32,+sm_61" } +//. +// CHECK: !0 = !{ptr @foo, !"kernel", i32 1} +// CHECK: !1 = !{i32 1, !"wchar_size", i32 4} +// CHECK: !2 = !{!"clang version 17.0.0"} +//. Index: clang/lib/Sema/SemaDeclAttr.cpp =================================================================== --- clang/lib/Sema/SemaDeclAttr.cpp +++ clang/lib/Sema/SemaDeclAttr.cpp @@ -4970,7 +4970,10 @@ if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice) S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD; - D->addAttr(::new (S.Context) CUDAGlobalAttr(S.Context, AL)); + if (AL.getKind() == ParsedAttr::AT_NVPTXKernel) + D->addAttr(::new (S.Context) NVPTXKernelAttr(S.Context, AL)); + else + D->addAttr(::new (S.Context) CUDAGlobalAttr(S.Context, AL)); // In host compilation the kernel is emitted as a stub function, which is // a helper function for launching the kernel. The instructions in the helper // function has nothing to do with the source code of the kernel. Do not emit @@ -8851,6 +8854,7 @@ case ParsedAttr::AT_CalledOnce: handleCalledOnceAttr(S, D, AL); break; + case ParsedAttr::AT_NVPTXKernel: case ParsedAttr::AT_CUDAGlobal: handleGlobalAttr(S, D, AL); break; Index: clang/lib/CodeGen/TargetInfo.cpp =================================================================== --- clang/lib/CodeGen/TargetInfo.cpp +++ clang/lib/CodeGen/TargetInfo.cpp @@ -7373,6 +7373,11 @@ } } } + + // Attach kernel metadata directly if compiling for NVPTX. + if (FD->hasAttr<NVPTXKernelAttr>()) { + addNVVMMetadata(F, "kernel", 1); + } } void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::GlobalValue *GV, Index: clang/include/clang/Basic/Attr.td =================================================================== --- clang/include/clang/Basic/Attr.td +++ clang/include/clang/Basic/Attr.td @@ -414,6 +414,7 @@ def TargetX86 : TargetArch<["x86"]>; def TargetAnyX86 : TargetArch<["x86", "x86_64"]>; def TargetWebAssembly : TargetArch<["wasm32", "wasm64"]>; +def TargetNVPTX : TargetArch<["nvptx", "nvptx64"]>; def TargetWindows : TargetSpec { let OSes = ["Win32"]; } @@ -1221,6 +1222,12 @@ } def : MutualExclusions<[CUDAGlobal, CUDAHost]>; +def NVPTXKernel : InheritableAttr, TargetSpecificAttr<TargetNVPTX> { + let Spellings = [Clang<"nvptx_kernel">]; + let Subjects = SubjectList<[Function]>; + let Documentation = [Undocumented]; +} + def HIPManaged : InheritableAttr { let Spellings = [GNU<"managed">, Declspec<"__managed__">]; let Subjects = SubjectList<[Var]>;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits