Skip to content

Commit b6328db

Browse files
authored
[clang][CodeGen] Put constant initializer globals into constant addrspace (#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.
1 parent f17b1fb commit b6328db

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

clang/lib/CodeGen/CGExprAgg.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,20 +537,23 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType,
537537
elementType.isTriviallyCopyableType(CGF.getContext())) {
538538
CodeGen::CodeGenModule &CGM = CGF.CGM;
539539
ConstantEmitter Emitter(CGF);
540-
LangAS AS = ArrayQTy.getAddressSpace();
540+
QualType GVArrayQTy = CGM.getContext().getAddrSpaceQualType(
541+
CGM.getContext().removeAddrSpaceQualType(ArrayQTy),
542+
CGM.GetGlobalConstantAddressSpace());
543+
LangAS AS = GVArrayQTy.getAddressSpace();
541544
if (llvm::Constant *C =
542-
Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) {
545+
Emitter.tryEmitForInitializer(ExprToVisit, AS, GVArrayQTy)) {
543546
auto GV = new llvm::GlobalVariable(
544547
CGM.getModule(), C->getType(),
545548
/* isConstant= */ true, llvm::GlobalValue::PrivateLinkage, C,
546549
"constinit",
547550
/* InsertBefore= */ nullptr, llvm::GlobalVariable::NotThreadLocal,
548551
CGM.getContext().getTargetAddressSpace(AS));
549552
Emitter.finalize(GV);
550-
CharUnits Align = CGM.getContext().getTypeAlignInChars(ArrayQTy);
553+
CharUnits Align = CGM.getContext().getTypeAlignInChars(GVArrayQTy);
551554
GV->setAlignment(Align.getAsAlign());
552555
Address GVAddr(GV, GV->getValueType(), Align);
553-
EmitFinalDestCopy(ArrayQTy, CGF.MakeAddrLValue(GVAddr, ArrayQTy));
556+
EmitFinalDestCopy(ArrayQTy, CGF.MakeAddrLValue(GVAddr, GVArrayQTy));
554557
return;
555558
}
556559
}

clang/test/CodeGenOpenCLCXX/addrspace-with-class.clcpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// for constructors, member functions and destructors.
66
// See also atexit.cl and global_init.cl for other specific tests.
77

8-
// CHECK: %struct.MyType = type { i32 }
8+
// CHECK: %struct.MyType = type { i32, [5 x i32] }
99
struct MyType {
1010
MyType(int i) : i(i) {}
1111
MyType(int i) __constant : i(i) {}
@@ -14,6 +14,7 @@ struct MyType {
1414
int bar() { return i + 2; }
1515
int bar() __constant { return i + 1; }
1616
int i;
17+
int a[5] = {42, 43, 44, 45, 46};
1718
};
1819

1920
// CHECK: @const1 ={{.*}} addrspace(2) global %struct.MyType zeroinitializer
@@ -23,6 +24,8 @@ __constant MyType const2(2);
2324
// CHECK: @glob ={{.*}} addrspace(1) global %struct.MyType zeroinitializer
2425
MyType glob(1);
2526

27+
// CHECK: @constinit ={{.*}} addrspace(2) constant [5 x i32] [i32 42, i32 43, i32 44, i32 45, i32 46]
28+
2629
// CHECK: call spir_func void @_ZNU3AS26MyTypeC1Ei(ptr addrspace(2) {{[^,]*}} @const1, i32 noundef 1)
2730
// CHECK: call spir_func void @_ZNU3AS26MyTypeC1Ei(ptr addrspace(2) {{[^,]*}} @const2, i32 noundef 2)
2831
// CHECK: call spir_func void @_ZNU3AS46MyTypeC1Ei(ptr addrspace(4) {{[^,]*}} addrspacecast (ptr addrspace(1) @glob to ptr addrspace(4)), i32 noundef 1)

0 commit comments

Comments
 (0)