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 <sven.vanhaastr...@arm.com> 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 &CGM = 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 <sven.vanhaastr...@arm.com> 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 &CGM = 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