[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -535,20 +535,23 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); -LangAS AS = ArrayQTy.getAddressSpace(); +QualType GVArrayQTy = CGM.getContext().getAddrSpaceQualType( +CGM.getContext().removeAddrSpaceQualType(ArrayQTy), changpeng wrote: > I think it's a bug in removeAddrSpaceQualType(): it needs to special-case > arrays. Arrays are weird because qualifiers on the element type also count as > qualifiers on the type, so getSingleStepDesugaredType() can't remove the > sugar on arrays. So it needs to strip the qualifier off the element type, > then reconstruct the array type. Maybe it can use > ASTContext::getUnqualifiedArrayType. Thanks for the suggestion. I drafted a fix: https://github.com/llvm/llvm-project/pull/92612 https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -535,20 +535,23 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); -LangAS AS = ArrayQTy.getAddressSpace(); +QualType GVArrayQTy = CGM.getContext().getAddrSpaceQualType( +CGM.getContext().removeAddrSpaceQualType(ArrayQTy), changpeng wrote: Reduced further: clang -c -Xclang -emit-llvm -O0 test.clcpp __kernel void test(__global ulong *In, __global ulong *Out) { ulong m[4] = { In[0], In[1], 0, 0 }; *Out = m[1]; } https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
changpeng wrote: [test.cl.txt](https://github.com/llvm/llvm-project/files/15355457/test.cl.txt) https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -535,20 +535,23 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); -LangAS AS = ArrayQTy.getAddressSpace(); +QualType GVArrayQTy = CGM.getContext().getAddrSpaceQualType( +CGM.getContext().removeAddrSpaceQualType(ArrayQTy), changpeng wrote: > @changpeng would you be able to provide an input source that demonstrates the > issue? Hi, @svenvh : I attached test.cl.txt here which is the dumped opencl source file. Unfortunately I do not know exactly how to reproduce the infinite loop offline with this source. I extracted out the following simplified kernel which can reproduce the hang with clang -c -Xclang -emit-llvm -O0 test.clcpp __kernel void nonceGrind(__global ulong *headerIn, __global ulong *nonceOut) { ulong m[16] = {headerIn[0], headerIn[1], headerIn[2], headerIn[3], 0, headerIn[5], headerIn[6], headerIn[7], headerIn[8], headerIn[9], 0, 0, 0, 0, 0, 0 }; *nonceOut = m[4]; } However, I am afraid it may not fully represent the original issue. This is because after I break out the loop in ASTContext::removeAddrSpaceQualType, I am seeing the following assert: clang: /home/chfang/llvm-project/clang/include/clang/AST/Type.h:677: void clang::Qualifiers::addConsistentQualifiers(Qualifiers): Assertion `getAddressSpace() == qs.getAddressSpace() || !hasAddressSpace() || !qs.hasAddressSpace()' failed. Hopefully the information is useful, and you are able to help. Thanks. https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -535,20 +535,23 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); -LangAS AS = ArrayQTy.getAddressSpace(); +QualType GVArrayQTy = CGM.getContext().getAddrSpaceQualType( +CGM.getContext().removeAddrSpaceQualType(ArrayQTy), changpeng wrote: > I think it's a bug in removeAddrSpaceQualType(): it needs to special-case > arrays. Arrays are weird because qualifiers on the element type also count as > qualifiers on the type, so getSingleStepDesugaredType() can't remove the > sugar on arrays. So it needs to strip the qualifier off the element type, > then reconstruct the array type. Maybe it can use ASTC getSingleStepDesugaredType Yes, the issue is in removeAddrSpaceQualType(ArrayQTy), And getSingleStepDesugaredType can not remove "Sugar". https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -535,20 +535,23 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); -LangAS AS = ArrayQTy.getAddressSpace(); +QualType GVArrayQTy = CGM.getContext().getAddrSpaceQualType( +CGM.getContext().removeAddrSpaceQualType(ArrayQTy), svenvh wrote: @changpeng would you be able to provide an input source that demonstrates the issue? https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -535,20 +535,23 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); -LangAS AS = ArrayQTy.getAddressSpace(); +QualType GVArrayQTy = CGM.getContext().getAddrSpaceQualType( +CGM.getContext().removeAddrSpaceQualType(ArrayQTy), efriedma-quic wrote: I think it's a bug in removeAddrSpaceQualType(): it needs to special-case arrays. Arrays are weird because qualifiers on the element type also count as qualifiers on the type, so getSingleStepDesugaredType() can't remove the sugar on arrays. So it needs to strip the qualifier off the element type, then reconstruct the array type. Maybe it can use ASTContext::getUnqualifiedArrayType. https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -535,20 +535,23 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); -LangAS AS = ArrayQTy.getAddressSpace(); +QualType GVArrayQTy = CGM.getContext().getAddrSpaceQualType( +CGM.getContext().removeAddrSpaceQualType(ArrayQTy), changpeng wrote: We saw a regression caused by this PR. It is a soft hang in CGM.getContext().removeAddrSpaceQualType. Specifically it is in the following while loop: while (T.hasAddressSpace()) { TypeNode = Quals.strip(T); // If the type no longer has an address space after stripping qualifiers, // jump out. if (!QualType(TypeNode, 0).hasAddressSpace()) break; // There might be sugar in the way. Strip it and try again. T = T.getSingleStepDesugaredType(*this); } We found that "T == T.getSingleStepDesugaredType(*this);" and this it could not proceed. I am not sure whether we should break out this loop when "T == T.getSingleStepDesugaredType(*this)" or something else is wrong that we should never see such case. Here is the dump of T: ConstantArrayType 0x65b40640 '__private ulong[16]' 16 `-QualType 0x65b403f8 '__private ulong' __private `-ElaboratedType 0x65b3ff40 'ulong' sugar imported `-TypedefType 0x65b3fef0 'ulong' sugar imported |-Typedef 0x65b3fe80 'ulong' `-BuiltinType 0x6583f430 'unsigned long' https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
https://github.com/svenvh closed https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
https://github.com/efriedma-quic approved this pull request. LGTM https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -535,20 +535,24 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); -LangAS AS = ArrayQTy.getAddressSpace(); +QualType GVArrayQTy = ArrayQTy; +if (!GVArrayQTy.hasAddressSpace()) + GVArrayQTy = CGM.getContext().getAddrSpaceQualType( svenvh wrote: Thanks, updated. https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
https://github.com/svenvh updated https://github.com/llvm/llvm-project/pull/90048 >From c5e7b2d5936a7317ebc33159b4cb72bf2aa66cf9 Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Thu, 25 Apr 2024 14:10:19 +0100 Subject: [PATCH 1/4] [OpenCL] Put constant initializer globals into constant addrspace Place constant initializer globals into the constant address space. Clang generates such globals for e.g. larger array member initializers of classes and then emits copy operations from the global to the object(s). The globals are never written so they ought to be in the constant address space. --- clang/lib/CodeGen/CGExprAgg.cpp| 2 ++ clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp | 5 - 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index 355fec42be4489..30cde245cc837c 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { auto GV = new llvm::GlobalVariable( diff --git a/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp b/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp index 18d97a989a4364..a0ed03b25535c8 100644 --- a/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp +++ b/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp @@ -5,7 +5,7 @@ // for constructors, member functions and destructors. // See also atexit.cl and global_init.cl for other specific tests. -// CHECK: %struct.MyType = type { i32 } +// CHECK: %struct.MyType = type { i32, [5 x i32] } struct MyType { MyType(int i) : i(i) {} MyType(int i) __constant : i(i) {} @@ -14,6 +14,7 @@ struct MyType { int bar() { return i + 2; } int bar() __constant { return i + 1; } int i; + int a[5] = {42, 43, 44, 45, 46}; }; // CHECK: @const1 ={{.*}} addrspace(2) global %struct.MyType zeroinitializer @@ -23,6 +24,8 @@ __constant MyType const2(2); // CHECK: @glob ={{.*}} addrspace(1) global %struct.MyType zeroinitializer MyType glob(1); +// CHECK: @constinit ={{.*}} addrspace(2) constant [5 x i32] [i32 42, i32 43, i32 44, i32 45, i32 46] + // CHECK: call spir_func void @_ZNU3AS26MyTypeC1Ei(ptr addrspace(2) {{[^,]*}} @const1, i32 noundef 1) // CHECK: call spir_func void @_ZNU3AS26MyTypeC1Ei(ptr addrspace(2) {{[^,]*}} @const2, i32 noundef 2) // CHECK: call spir_func void @_ZNU3AS46MyTypeC1Ei(ptr addrspace(4) {{[^,]*}} addrspacecast (ptr addrspace(1) @glob to ptr addrspace(4)), i32 noundef 1) >From 08936e55847bd866a1a602a2d7b7c7de5a603df8 Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Fri, 26 Apr 2024 13:56:25 +0100 Subject: [PATCH 2/4] Address code review comment: set ArrayQTy addrspace --- clang/lib/CodeGen/CGExprAgg.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index 30cde245cc837c..44514c830b09cf 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -535,9 +535,10 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); +if (CGF.getLangOpts().OpenCL && !ArrayQTy.hasAddressSpace()) + ArrayQTy = CGM.getContext().getAddrSpaceQualType(ArrayQTy, + LangAS::opencl_constant); LangAS AS = ArrayQTy.getAddressSpace(); -if (CGF.getLangOpts().OpenCL) - AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { auto GV = new llvm::GlobalVariable( >From ebefe4bf61db905748da330a22aeb8782ef8cebf Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Mon, 29 Apr 2024 13:18:53 +0100 Subject: [PATCH 3/4] Address review comment: use GetGlobalConstantAddressSpace and extract GVArrayQTy --- clang/lib/CodeGen/CGExprAgg.cpp | 15 --- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index 44514c830b09cf..41a183ac829949 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -535,12 +535,13 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); -if (CGF.getLangOpts().OpenCL && !ArrayQTy.hasAddressSpace()) - ArrayQTy =
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -535,20 +535,24 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); -LangAS AS = ArrayQTy.getAddressSpace(); +QualType GVArrayQTy = ArrayQTy; +if (!GVArrayQTy.hasAddressSpace()) + GVArrayQTy = CGM.getContext().getAddrSpaceQualType( efriedma-quic wrote: I think you just just want `GVArrayQTy = CGM.getContext().getAddrSpaceQualType(CGM.getContext().removeAddrSpaceQualType(GVArrayQTy), CGM.GetGlobalConstantAddressSpace());`, without the "if": you want to change the address-space even if the address-space of the destination is explicitly specified. https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { auto GV = new llvm::GlobalVariable( svenvh wrote: Nice catch, that wasn't obvious to me from the code. Updated by splitting out a separate QualType variable for the global. https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; svenvh wrote: Thanks for the suggestion; updated to use `GetGlobalConstantAddressSpace()`. https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
https://github.com/svenvh updated https://github.com/llvm/llvm-project/pull/90048 >From c5e7b2d5936a7317ebc33159b4cb72bf2aa66cf9 Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Thu, 25 Apr 2024 14:10:19 +0100 Subject: [PATCH 1/3] [OpenCL] Put constant initializer globals into constant addrspace Place constant initializer globals into the constant address space. Clang generates such globals for e.g. larger array member initializers of classes and then emits copy operations from the global to the object(s). The globals are never written so they ought to be in the constant address space. --- clang/lib/CodeGen/CGExprAgg.cpp| 2 ++ clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp | 5 - 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index 355fec42be4489..30cde245cc837c 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { auto GV = new llvm::GlobalVariable( diff --git a/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp b/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp index 18d97a989a4364..a0ed03b25535c8 100644 --- a/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp +++ b/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp @@ -5,7 +5,7 @@ // for constructors, member functions and destructors. // See also atexit.cl and global_init.cl for other specific tests. -// CHECK: %struct.MyType = type { i32 } +// CHECK: %struct.MyType = type { i32, [5 x i32] } struct MyType { MyType(int i) : i(i) {} MyType(int i) __constant : i(i) {} @@ -14,6 +14,7 @@ struct MyType { int bar() { return i + 2; } int bar() __constant { return i + 1; } int i; + int a[5] = {42, 43, 44, 45, 46}; }; // CHECK: @const1 ={{.*}} addrspace(2) global %struct.MyType zeroinitializer @@ -23,6 +24,8 @@ __constant MyType const2(2); // CHECK: @glob ={{.*}} addrspace(1) global %struct.MyType zeroinitializer MyType glob(1); +// CHECK: @constinit ={{.*}} addrspace(2) constant [5 x i32] [i32 42, i32 43, i32 44, i32 45, i32 46] + // CHECK: call spir_func void @_ZNU3AS26MyTypeC1Ei(ptr addrspace(2) {{[^,]*}} @const1, i32 noundef 1) // CHECK: call spir_func void @_ZNU3AS26MyTypeC1Ei(ptr addrspace(2) {{[^,]*}} @const2, i32 noundef 2) // CHECK: call spir_func void @_ZNU3AS46MyTypeC1Ei(ptr addrspace(4) {{[^,]*}} addrspacecast (ptr addrspace(1) @glob to ptr addrspace(4)), i32 noundef 1) >From 08936e55847bd866a1a602a2d7b7c7de5a603df8 Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Fri, 26 Apr 2024 13:56:25 +0100 Subject: [PATCH 2/3] Address code review comment: set ArrayQTy addrspace --- clang/lib/CodeGen/CGExprAgg.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index 30cde245cc837c..44514c830b09cf 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -535,9 +535,10 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); +if (CGF.getLangOpts().OpenCL && !ArrayQTy.hasAddressSpace()) + ArrayQTy = CGM.getContext().getAddrSpaceQualType(ArrayQTy, + LangAS::opencl_constant); LangAS AS = ArrayQTy.getAddressSpace(); -if (CGF.getLangOpts().OpenCL) - AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { auto GV = new llvm::GlobalVariable( >From ebefe4bf61db905748da330a22aeb8782ef8cebf Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Mon, 29 Apr 2024 13:18:53 +0100 Subject: [PATCH 3/3] Address review comment: use GetGlobalConstantAddressSpace and extract GVArrayQTy --- clang/lib/CodeGen/CGExprAgg.cpp | 15 --- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index 44514c830b09cf..41a183ac829949 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -535,12 +535,13 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); -if (CGF.getLangOpts().OpenCL && !ArrayQTy.hasAddressSpace()) - ArrayQTy =
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; efriedma-quic wrote: That's true as far as it goes, but I'd prefer to make this a bit more general... It looks like there's an existing GetGlobalConstantAddressSpace() which computes what we want here? https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { auto GV = new llvm::GlobalVariable( efriedma-quic wrote: The first argument EmitFinalDestCopy is the type of the destination, not the source. https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
https://github.com/svenvh updated https://github.com/llvm/llvm-project/pull/90048 >From c5e7b2d5936a7317ebc33159b4cb72bf2aa66cf9 Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Thu, 25 Apr 2024 14:10:19 +0100 Subject: [PATCH 1/2] [OpenCL] Put constant initializer globals into constant addrspace Place constant initializer globals into the constant address space. Clang generates such globals for e.g. larger array member initializers of classes and then emits copy operations from the global to the object(s). The globals are never written so they ought to be in the constant address space. --- clang/lib/CodeGen/CGExprAgg.cpp| 2 ++ clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp | 5 - 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index 355fec42be4489..30cde245cc837c 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { auto GV = new llvm::GlobalVariable( diff --git a/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp b/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp index 18d97a989a4364..a0ed03b25535c8 100644 --- a/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp +++ b/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp @@ -5,7 +5,7 @@ // for constructors, member functions and destructors. // See also atexit.cl and global_init.cl for other specific tests. -// CHECK: %struct.MyType = type { i32 } +// CHECK: %struct.MyType = type { i32, [5 x i32] } struct MyType { MyType(int i) : i(i) {} MyType(int i) __constant : i(i) {} @@ -14,6 +14,7 @@ struct MyType { int bar() { return i + 2; } int bar() __constant { return i + 1; } int i; + int a[5] = {42, 43, 44, 45, 46}; }; // CHECK: @const1 ={{.*}} addrspace(2) global %struct.MyType zeroinitializer @@ -23,6 +24,8 @@ __constant MyType const2(2); // CHECK: @glob ={{.*}} addrspace(1) global %struct.MyType zeroinitializer MyType glob(1); +// CHECK: @constinit ={{.*}} addrspace(2) constant [5 x i32] [i32 42, i32 43, i32 44, i32 45, i32 46] + // CHECK: call spir_func void @_ZNU3AS26MyTypeC1Ei(ptr addrspace(2) {{[^,]*}} @const1, i32 noundef 1) // CHECK: call spir_func void @_ZNU3AS26MyTypeC1Ei(ptr addrspace(2) {{[^,]*}} @const2, i32 noundef 2) // CHECK: call spir_func void @_ZNU3AS46MyTypeC1Ei(ptr addrspace(4) {{[^,]*}} addrspacecast (ptr addrspace(1) @glob to ptr addrspace(4)), i32 noundef 1) >From 08936e55847bd866a1a602a2d7b7c7de5a603df8 Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Fri, 26 Apr 2024 13:56:25 +0100 Subject: [PATCH 2/2] Address code review comment: set ArrayQTy addrspace --- clang/lib/CodeGen/CGExprAgg.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index 30cde245cc837c..44514c830b09cf 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -535,9 +535,10 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, elementType.isTriviallyCopyableType(CGF.getContext())) { CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); +if (CGF.getLangOpts().OpenCL && !ArrayQTy.hasAddressSpace()) + ArrayQTy = CGM.getContext().getAddrSpaceQualType(ArrayQTy, + LangAS::opencl_constant); LangAS AS = ArrayQTy.getAddressSpace(); -if (CGF.getLangOpts().OpenCL) - AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { auto GV = new llvm::GlobalVariable( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { auto GV = new llvm::GlobalVariable( svenvh wrote: It seems to have worked indeed, but we should probably adjust the address space of `ArrayQTy` everywhere then? I'll upload a new revision to do so. https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; svenvh wrote: The OpenCL language defines the constant address space for read-only variables. Targets can decide for themselves how to map the language address space to a target address space (which is done further down at the GlobalVariable creation). https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { efriedma-quic wrote: (tryEmitForInitializer doesn't really use the AS; I'll post a patch to get rid of it. Doesn't directly impact this patch, I guess.) https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; efriedma-quic wrote: Why is this OpenCL specific? At first glance, this should be independent of language dialect; the most efficient address-space to store constants depends on the target. https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
@@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { auto GV = new llvm::GlobalVariable( efriedma-quic wrote: In the call to MakeAddrLValue, do we need to fix the address-space of ArrayQTy? Maybe it happens to work at the moment because we infer the address-space for the memcpy itself from the LLVM pointer type. But I'd prefer to fix it properly in case it ends up mattering in the future. https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
llvmbot wrote: @llvm/pr-subscribers-clang-codegen Author: Sven van Haastregt (svenvh) Changes Place constant initializer globals into the constant address space. Clang generates such globals for e.g. larger array member initializers of classes and then emits copy operations from the global to the object(s). The globals are never written so they ought to be in the constant address space. --- Full diff: https://github.com/llvm/llvm-project/pull/90048.diff 2 Files Affected: - (modified) clang/lib/CodeGen/CGExprAgg.cpp (+2) - (modified) clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp (+4-1) ``diff diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index 355fec42be4489..30cde245cc837c 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { auto GV = new llvm::GlobalVariable( diff --git a/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp b/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp index 18d97a989a4364..a0ed03b25535c8 100644 --- a/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp +++ b/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp @@ -5,7 +5,7 @@ // for constructors, member functions and destructors. // See also atexit.cl and global_init.cl for other specific tests. -// CHECK: %struct.MyType = type { i32 } +// CHECK: %struct.MyType = type { i32, [5 x i32] } struct MyType { MyType(int i) : i(i) {} MyType(int i) __constant : i(i) {} @@ -14,6 +14,7 @@ struct MyType { int bar() { return i + 2; } int bar() __constant { return i + 1; } int i; + int a[5] = {42, 43, 44, 45, 46}; }; // CHECK: @const1 ={{.*}} addrspace(2) global %struct.MyType zeroinitializer @@ -23,6 +24,8 @@ __constant MyType const2(2); // CHECK: @glob ={{.*}} addrspace(1) global %struct.MyType zeroinitializer MyType glob(1); +// CHECK: @constinit ={{.*}} addrspace(2) constant [5 x i32] [i32 42, i32 43, i32 44, i32 45, i32 46] + // CHECK: call spir_func void @_ZNU3AS26MyTypeC1Ei(ptr addrspace(2) {{[^,]*}} @const1, i32 noundef 1) // CHECK: call spir_func void @_ZNU3AS26MyTypeC1Ei(ptr addrspace(2) {{[^,]*}} @const2, i32 noundef 2) // CHECK: call spir_func void @_ZNU3AS46MyTypeC1Ei(ptr addrspace(4) {{[^,]*}} addrspacecast (ptr addrspace(1) @glob to ptr addrspace(4)), i32 noundef 1) `` https://github.com/llvm/llvm-project/pull/90048 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [OpenCL] Put constant initializer globals into constant addrspace (PR #90048)
https://github.com/svenvh created https://github.com/llvm/llvm-project/pull/90048 Place constant initializer globals into the constant address space. Clang generates such globals for e.g. larger array member initializers of classes and then emits copy operations from the global to the object(s). The globals are never written so they ought to be in the constant address space. >From c5e7b2d5936a7317ebc33159b4cb72bf2aa66cf9 Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Thu, 25 Apr 2024 14:10:19 +0100 Subject: [PATCH] [OpenCL] Put constant initializer globals into constant addrspace Place constant initializer globals into the constant address space. Clang generates such globals for e.g. larger array member initializers of classes and then emits copy operations from the global to the object(s). The globals are never written so they ought to be in the constant address space. --- clang/lib/CodeGen/CGExprAgg.cpp| 2 ++ clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp | 5 - 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index 355fec42be4489..30cde245cc837c 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -536,6 +536,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType, CodeGen::CodeGenModule = CGF.CGM; ConstantEmitter Emitter(CGF); LangAS AS = ArrayQTy.getAddressSpace(); +if (CGF.getLangOpts().OpenCL) + AS = LangAS::opencl_constant; if (llvm::Constant *C = Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) { auto GV = new llvm::GlobalVariable( diff --git a/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp b/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp index 18d97a989a4364..a0ed03b25535c8 100644 --- a/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp +++ b/clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp @@ -5,7 +5,7 @@ // for constructors, member functions and destructors. // See also atexit.cl and global_init.cl for other specific tests. -// CHECK: %struct.MyType = type { i32 } +// CHECK: %struct.MyType = type { i32, [5 x i32] } struct MyType { MyType(int i) : i(i) {} MyType(int i) __constant : i(i) {} @@ -14,6 +14,7 @@ struct MyType { int bar() { return i + 2; } int bar() __constant { return i + 1; } int i; + int a[5] = {42, 43, 44, 45, 46}; }; // CHECK: @const1 ={{.*}} addrspace(2) global %struct.MyType zeroinitializer @@ -23,6 +24,8 @@ __constant MyType const2(2); // CHECK: @glob ={{.*}} addrspace(1) global %struct.MyType zeroinitializer MyType glob(1); +// CHECK: @constinit ={{.*}} addrspace(2) constant [5 x i32] [i32 42, i32 43, i32 44, i32 45, i32 46] + // CHECK: call spir_func void @_ZNU3AS26MyTypeC1Ei(ptr addrspace(2) {{[^,]*}} @const1, i32 noundef 1) // CHECK: call spir_func void @_ZNU3AS26MyTypeC1Ei(ptr addrspace(2) {{[^,]*}} @const2, i32 noundef 2) // CHECK: call spir_func void @_ZNU3AS46MyTypeC1Ei(ptr addrspace(4) {{[^,]*}} addrspacecast (ptr addrspace(1) @glob to ptr addrspace(4)), i32 noundef 1) ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits