Skip to content

Commit 3bff980

Browse files
ghehglanza
authored andcommitted
[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 c246557 commit 3bff980

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ void CIRGenModule::buildGlobalVarDefinition(const clang::VarDecl *D,
13291329
setTLSMode(GV, *D);
13301330
}
13311331

1332-
// TODO(cir): maybeSetTrivialComdat(*D, *GV);
1332+
maybeSetTrivialComdat(*D, GV);
13331333

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

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

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 1 addition & 1 deletion
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

Lines changed: 19 additions & 12 deletions
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

Lines changed: 1 addition & 1 deletion
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)