[llvm] [compiler-rt] [flang] [clang-tools-extra] [clang] [mlir] [clang][CodeGen] Handle template parameter objects with explicit address spaces (PR #69266)
Alex Voicu via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 28 07:05:42 PST 2023
https://github.com/AlexVlx updated https://github.com/llvm/llvm-project/pull/69266
>From ded7435220d2c3527c4798d1b328a5f2940e279a Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Mon, 16 Oct 2023 22:43:55 +0100
Subject: [PATCH 1/3] Handle trying to bind a generic reference to a template
parameter object value that is in an explicit address space.
---
clang/lib/CodeGen/CGExpr.cpp | 18 +++++++++--
.../template-param-objects-address-space.cpp | 32 +++++++++++++++++++
2 files changed, 47 insertions(+), 3 deletions(-)
create mode 100644 clang/test/CodeGenCXX/template-param-objects-address-space.cpp
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 54a1d300a9ac738..784d3f7b03909e3 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -2992,9 +2992,21 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
return MakeAddrLValue(CGM.GetAddrOfMSGuidDecl(GD), T,
AlignmentSource::Decl);
- if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND))
- return MakeAddrLValue(CGM.GetAddrOfTemplateParamObject(TPO), T,
- AlignmentSource::Decl);
+ if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND)) {
+ auto ATPO = CGM.GetAddrOfTemplateParamObject(TPO);
+ auto AS = getLangASFromTargetAS(ATPO.getAddressSpace());
+
+ if (AS != T.getAddressSpace()) {
+ auto TargetAS = getContext().getTargetAddressSpace(T.getAddressSpace());
+ auto PtrTy = ATPO.getElementType()->getPointerTo(TargetAS);
+ auto ASC = getTargetHooks().performAddrSpaceCast(CGM, ATPO.getPointer(),
+ AS, T.getAddressSpace(),
+ PtrTy);
+ ATPO = ConstantAddress(ASC, ATPO.getElementType(), ATPO.getAlignment());
+ }
+
+ return MakeAddrLValue(ATPO, T, AlignmentSource::Decl);
+ }
llvm_unreachable("Unhandled DeclRefExpr");
}
diff --git a/clang/test/CodeGenCXX/template-param-objects-address-space.cpp b/clang/test/CodeGenCXX/template-param-objects-address-space.cpp
new file mode 100644
index 000000000000000..b54dcfe77934ee2
--- /dev/null
+++ b/clang/test/CodeGenCXX/template-param-objects-address-space.cpp
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -std=c++20 %s -emit-llvm -o - | FileCheck %s
+
+struct S { char buf[32]; };
+template<S s> constexpr const char *begin() { return s.buf; }
+template<S s> constexpr const char *end() { return s.buf + __builtin_strlen(s.buf); }
+template<S s> constexpr const void *retval() { return &s; }
+extern const void *callee(const S*);
+template<S s> constexpr const void* observable_addr() { return callee(&s); }
+
+// CHECK: [[HELLO:@_ZTAXtl1StlA32_cLc104ELc101ELc108ELc108ELc111ELc32ELc119ELc111ELc114ELc108ELc100EEEE]]
+// CHECK-SAME: = linkonce_odr addrspace(1) constant { <{ [11 x i8], [21 x i8] }> } { <{ [11 x i8], [21 x i8] }> <{ [11 x i8] c"hello world", [21 x i8] zeroinitializer }> }, comdat
+
+// CHECK: @p
+// CHECK-SAME: addrspace(1) global ptr addrspacecast (ptr addrspace(1) [[HELLO]] to ptr)
+const char *p = begin<S{"hello world"}>();
+
+// CHECK: @q
+// CHECK-SAME: addrspace(1) global ptr addrspacecast (ptr addrspace(1) getelementptr (i8, ptr addrspace(1) [[HELLO]], i64 11) to ptr)
+const char *q = end<S{"hello world"}>();
+
+const void *(*r)() = &retval<S{"hello world"}>;
+
+// CHECK: @s
+// CHECK-SAME: addrspace(1) global ptr null
+const void *s = observable_addr<S{"hello world"}>();
+
+// CHECK: define linkonce_odr noundef ptr @_Z6retvalIXtl1StlA32_cLc104ELc101ELc108ELc108ELc111ELc32ELc119ELc111ELc114ELc108ELc100EEEEEPKvv()
+// CHECK: ret ptr addrspacecast (ptr addrspace(1) [[HELLO]] to ptr)
+
+// CHECK: define linkonce_odr noundef ptr @_Z15observable_addrIXtl1StlA32_cLc104ELc101ELc108ELc108ELc111ELc32ELc119ELc111ELc114ELc108ELc100EEEEEPKvv()
+// CHECK: %call = call noundef ptr @_Z6calleePK1S(ptr noundef addrspacecast (ptr addrspace(1) [[HELLO]] to ptr))
+// CHECK: declare noundef ptr @_Z6calleePK1S(ptr noundef)
>From 4afd54856ca8248fab731e17cd644d18ed60acbc Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Thu, 9 Nov 2023 14:51:39 -1000
Subject: [PATCH 2/3] Fix formatting error.
---
clang/lib/CodeGen/CGExpr.cpp | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 652d9c32a8c47b4..8abb1d8a1be4e97 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -3045,9 +3045,8 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
if (AS != T.getAddressSpace()) {
auto TargetAS = getContext().getTargetAddressSpace(T.getAddressSpace());
auto PtrTy = ATPO.getElementType()->getPointerTo(TargetAS);
- auto ASC = getTargetHooks().performAddrSpaceCast(CGM, ATPO.getPointer(),
- AS, T.getAddressSpace(),
- PtrTy);
+ auto ASC = getTargetHooks().performAddrSpaceCast(
+ CGM, ATPO.getPointer(), AS, T.getAddressSpace(), PtrTy);
ATPO = ConstantAddress(ASC, ATPO.getElementType(), ATPO.getAlignment());
}
>From 5ba011e251efb6bc973006f51fb0888965900760 Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Thu, 9 Nov 2023 15:35:07 -1000
Subject: [PATCH 3/3] Really fix formatting error.
---
clang/lib/CodeGen/CGExpr.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 8abb1d8a1be4e97..9fe8f1d7da780c8 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -3046,7 +3046,7 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
auto TargetAS = getContext().getTargetAddressSpace(T.getAddressSpace());
auto PtrTy = ATPO.getElementType()->getPointerTo(TargetAS);
auto ASC = getTargetHooks().performAddrSpaceCast(
- CGM, ATPO.getPointer(), AS, T.getAddressSpace(), PtrTy);
+ CGM, ATPO.getPointer(), AS, T.getAddressSpace(), PtrTy);
ATPO = ConstantAddress(ASC, ATPO.getElementType(), ATPO.getAlignment());
}
More information about the cfe-commits
mailing list