Skip to content

Commit 52323c1

Browse files
authored
[CIR][CIRGen] Allow maybeSetTrivialComdat for GlobalOp (#885)
as title, this would complete solution to fix issue [LLVM lowering missing comdat and constant attributes](#801)
1 parent 37aafef commit 52323c1

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ void CIRGenModule::buildGlobalVarDefinition(const clang::VarDecl *D,
13281328
setTLSMode(GV, *D);
13291329
}
13301330

1331-
// TODO(cir): maybeSetTrivialComdat(*D, *GV);
1331+
maybeSetTrivialComdat(*D, GV);
13321332

13331333
// TODO(cir):
13341334
// Emit the initializer function if necessary.
@@ -3008,11 +3008,14 @@ bool CIRGenModule::supportsCOMDAT() const {
30083008
return getTriple().supportsCOMDAT();
30093009
}
30103010

3011-
void CIRGenModule::maybeSetTrivialComdat(const Decl &D, mlir::Operation *Op) {
3012-
if (!shouldBeInCOMDAT(*this, D))
3011+
void CIRGenModule::maybeSetTrivialComdat(const Decl &d, mlir::Operation *op) {
3012+
if (!shouldBeInCOMDAT(*this, d))
30133013
return;
3014-
3015-
// TODO: Op.setComdat
3014+
auto globalOp = dyn_cast_or_null<mlir::cir::GlobalOp>(op);
3015+
if (globalOp)
3016+
globalOp.setComdat(true);
3017+
// Keep it as missing feature as we need to implement comdat for FuncOp.
3018+
// in the future.
30163019
assert(!MissingFeatures::setComdat() && "NYI");
30173020
}
30183021

clang/lib/CIR/CodeGen/CIRGenModule.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ class CIRGenModule : public CIRGenTypeCache {
682682
clang::GlobalDecl &Result) const;
683683

684684
bool supportsCOMDAT() const;
685-
void maybeSetTrivialComdat(const clang::Decl &D, mlir::Operation *Op);
685+
void maybeSetTrivialComdat(const clang::Decl &d, mlir::Operation *op);
686686

687687
void emitError(const llvm::Twine &message) { theModule.emitError(message); }
688688

clang/test/CIR/CodeGen/cxx1z-inline-variables.cpp

+19-12
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,25 @@ const int &compat_use_after_redecl1 = compat::c;
2626
const int &compat_use_after_redecl2 = compat::d;
2727
const int &compat_use_after_redecl3 = compat::g;
2828

29-
// CIR: cir.global weak_odr @_ZN6compat1bE = #cir.int<2> : !s32i {alignment = 4 : i64}
30-
// CIR: cir.global weak_odr @_ZN6compat1aE = #cir.int<1> : !s32i {alignment = 4 : i64}
31-
// CIR: cir.global weak_odr @_ZN6compat1cE = #cir.int<3> : !s32i {alignment = 4 : i64}
29+
// CIR: cir.global weak_odr comdat @_ZN6compat1bE = #cir.int<2> : !s32i {alignment = 4 : i64}
30+
// CIR: cir.global weak_odr comdat @_ZN6compat1aE = #cir.int<1> : !s32i {alignment = 4 : i64}
31+
// CIR: cir.global weak_odr comdat @_ZN6compat1cE = #cir.int<3> : !s32i {alignment = 4 : i64}
3232
// CIR: cir.global external @_ZN6compat1eE = #cir.int<5> : !s32i {alignment = 4 : i64}
33-
// CIR: cir.global weak_odr @_ZN6compat1fE = #cir.int<6> : !s32i {alignment = 4 : i64}
34-
// CIR: cir.global linkonce_odr @_ZN6compat1dE = #cir.int<4> : !s32i {alignment = 4 : i64}
35-
// CIR: cir.global linkonce_odr @_ZN6compat1gE = #cir.int<7> : !s32i {alignment = 4 : i64}
33+
// CIR: cir.global weak_odr comdat @_ZN6compat1fE = #cir.int<6> : !s32i {alignment = 4 : i64}
34+
// CIR: cir.global linkonce_odr comdat @_ZN6compat1dE = #cir.int<4> : !s32i {alignment = 4 : i64}
35+
// CIR: cir.global linkonce_odr comdat @_ZN6compat1gE = #cir.int<7> : !s32i {alignment = 4 : i64}
3636

37-
// LLVM: @_ZN6compat1bE = weak_odr global i32 2, align 4
38-
// LLVM: @_ZN6compat1aE = weak_odr global i32 1, align 4
39-
// LLVM: @_ZN6compat1cE = weak_odr global i32 3, align 4
37+
// LLVM: $_ZN6compat1bE = comdat any
38+
// LLVM: $_ZN6compat1aE = comdat any
39+
// LLVM: $_ZN6compat1cE = comdat any
40+
// LLVM: $_ZN6compat1fE = comdat any
41+
// LLVM: $_ZN6compat1dE = comdat any
42+
// LLVM: $_ZN6compat1gE = comdat any
43+
44+
// LLVM: @_ZN6compat1bE = weak_odr global i32 2, comdat, align 4
45+
// LLVM: @_ZN6compat1aE = weak_odr global i32 1, comdat, align 4
46+
// LLVM: @_ZN6compat1cE = weak_odr global i32 3, comdat, align 4
4047
// LLVM: @_ZN6compat1eE = global i32 5, align 4
41-
// LLVM: @_ZN6compat1fE = weak_odr global i32 6, align 4
42-
// LLVM: @_ZN6compat1dE = linkonce_odr global i32 4, align 4
43-
// LLVM: @_ZN6compat1gE = linkonce_odr global i32 7, align 4
48+
// LLVM: @_ZN6compat1fE = weak_odr global i32 6, comdat, align 4
49+
// LLVM: @_ZN6compat1dE = linkonce_odr global i32 4, comdat, align 4
50+
// LLVM: @_ZN6compat1gE = linkonce_odr global i32 7, comdat, align 4

clang/test/CIR/CodeGen/weak.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void active (void)
2323
// LLVM-NEXT: call void @B()
2424

2525
int __attribute__((selectany)) y;
26-
// CIR: cir.global weak_odr @y
26+
// CIR: cir.global weak_odr comdat @y
2727

2828
int __attribute__((weak)) x;
2929
// CIR: cir.global weak

0 commit comments

Comments
 (0)