From 375732529be2ae174d00bcf2e42cd9a86472e383 Mon Sep 17 00:00:00 2001 From: ghehg Date: Wed, 25 Sep 2024 10:49:06 -0700 Subject: [PATCH] maybeSetTrivialComdat for GlobalOp --- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 13 +++++--- clang/lib/CIR/CodeGen/CIRGenModule.h | 2 +- .../CIR/CodeGen/cxx1z-inline-variables.cpp | 31 ++++++++++++------- clang/test/CIR/CodeGen/weak.c | 2 +- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index a8221b96fcbf..b1f052cd3e09 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -1328,7 +1328,7 @@ void CIRGenModule::buildGlobalVarDefinition(const clang::VarDecl *D, setTLSMode(GV, *D); } - // TODO(cir): maybeSetTrivialComdat(*D, *GV); + maybeSetTrivialComdat(*D, GV); // TODO(cir): // Emit the initializer function if necessary. @@ -3008,11 +3008,14 @@ bool CIRGenModule::supportsCOMDAT() const { return getTriple().supportsCOMDAT(); } -void CIRGenModule::maybeSetTrivialComdat(const Decl &D, mlir::Operation *Op) { - if (!shouldBeInCOMDAT(*this, D)) +void CIRGenModule::maybeSetTrivialComdat(const Decl &d, mlir::Operation *op) { + if (!shouldBeInCOMDAT(*this, d)) return; - - // TODO: Op.setComdat + auto globalOp = dyn_cast_or_null(op); + if (globalOp) + globalOp.setComdat(true); + // Keep it as missing feature as we need to implement comdat for FuncOp. + // in the future. assert(!MissingFeatures::setComdat() && "NYI"); } diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h index 1f55a83a767c..b980ed411c41 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.h +++ b/clang/lib/CIR/CodeGen/CIRGenModule.h @@ -682,7 +682,7 @@ class CIRGenModule : public CIRGenTypeCache { clang::GlobalDecl &Result) const; bool supportsCOMDAT() const; - void maybeSetTrivialComdat(const clang::Decl &D, mlir::Operation *Op); + void maybeSetTrivialComdat(const clang::Decl &d, mlir::Operation *op); void emitError(const llvm::Twine &message) { theModule.emitError(message); } diff --git a/clang/test/CIR/CodeGen/cxx1z-inline-variables.cpp b/clang/test/CIR/CodeGen/cxx1z-inline-variables.cpp index f54519d72650..68cddd578767 100644 --- a/clang/test/CIR/CodeGen/cxx1z-inline-variables.cpp +++ b/clang/test/CIR/CodeGen/cxx1z-inline-variables.cpp @@ -26,18 +26,25 @@ const int &compat_use_after_redecl1 = compat::c; const int &compat_use_after_redecl2 = compat::d; const int &compat_use_after_redecl3 = compat::g; -// CIR: cir.global weak_odr @_ZN6compat1bE = #cir.int<2> : !s32i {alignment = 4 : i64} -// CIR: cir.global weak_odr @_ZN6compat1aE = #cir.int<1> : !s32i {alignment = 4 : i64} -// CIR: cir.global weak_odr @_ZN6compat1cE = #cir.int<3> : !s32i {alignment = 4 : i64} +// CIR: cir.global weak_odr comdat @_ZN6compat1bE = #cir.int<2> : !s32i {alignment = 4 : i64} +// CIR: cir.global weak_odr comdat @_ZN6compat1aE = #cir.int<1> : !s32i {alignment = 4 : i64} +// CIR: cir.global weak_odr comdat @_ZN6compat1cE = #cir.int<3> : !s32i {alignment = 4 : i64} // CIR: cir.global external @_ZN6compat1eE = #cir.int<5> : !s32i {alignment = 4 : i64} -// CIR: cir.global weak_odr @_ZN6compat1fE = #cir.int<6> : !s32i {alignment = 4 : i64} -// CIR: cir.global linkonce_odr @_ZN6compat1dE = #cir.int<4> : !s32i {alignment = 4 : i64} -// CIR: cir.global linkonce_odr @_ZN6compat1gE = #cir.int<7> : !s32i {alignment = 4 : i64} +// CIR: cir.global weak_odr comdat @_ZN6compat1fE = #cir.int<6> : !s32i {alignment = 4 : i64} +// CIR: cir.global linkonce_odr comdat @_ZN6compat1dE = #cir.int<4> : !s32i {alignment = 4 : i64} +// CIR: cir.global linkonce_odr comdat @_ZN6compat1gE = #cir.int<7> : !s32i {alignment = 4 : i64} -// LLVM: @_ZN6compat1bE = weak_odr global i32 2, align 4 -// LLVM: @_ZN6compat1aE = weak_odr global i32 1, align 4 -// LLVM: @_ZN6compat1cE = weak_odr global i32 3, align 4 +// LLVM: $_ZN6compat1bE = comdat any +// LLVM: $_ZN6compat1aE = comdat any +// LLVM: $_ZN6compat1cE = comdat any +// LLVM: $_ZN6compat1fE = comdat any +// LLVM: $_ZN6compat1dE = comdat any +// LLVM: $_ZN6compat1gE = comdat any + +// LLVM: @_ZN6compat1bE = weak_odr global i32 2, comdat, align 4 +// LLVM: @_ZN6compat1aE = weak_odr global i32 1, comdat, align 4 +// LLVM: @_ZN6compat1cE = weak_odr global i32 3, comdat, align 4 // LLVM: @_ZN6compat1eE = global i32 5, align 4 -// LLVM: @_ZN6compat1fE = weak_odr global i32 6, align 4 -// LLVM: @_ZN6compat1dE = linkonce_odr global i32 4, align 4 -// LLVM: @_ZN6compat1gE = linkonce_odr global i32 7, align 4 +// LLVM: @_ZN6compat1fE = weak_odr global i32 6, comdat, align 4 +// LLVM: @_ZN6compat1dE = linkonce_odr global i32 4, comdat, align 4 +// LLVM: @_ZN6compat1gE = linkonce_odr global i32 7, comdat, align 4 diff --git a/clang/test/CIR/CodeGen/weak.c b/clang/test/CIR/CodeGen/weak.c index 25ebf15f8a89..398ac47c73f9 100644 --- a/clang/test/CIR/CodeGen/weak.c +++ b/clang/test/CIR/CodeGen/weak.c @@ -23,7 +23,7 @@ void active (void) // LLVM-NEXT: call void @B() int __attribute__((selectany)) y; -// CIR: cir.global weak_odr @y +// CIR: cir.global weak_odr comdat @y int __attribute__((weak)) x; // CIR: cir.global weak