Skip to content

[CIR][CIRGen] Fix "definition with same mangled name" error #1016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,20 +607,17 @@ void CIRGenModule::buildGlobalFunctionDefinition(GlobalDecl GD,
auto Ty = getTypes().GetFunctionType(FI);

// Get or create the prototype for the function.
// if (!V || (V.getValueType() != Ty))
// TODO(cir): Figure out what to do here? llvm uses a GlobalValue for the
// FuncOp in mlir
Op = GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/true,
ForDefinition);

auto globalVal = dyn_cast_or_null<mlir::cir::CIRGlobalValueInterface>(Op);
if (globalVal && !globalVal.isDeclaration()) {
// Already emitted.
auto Fn = dyn_cast_if_present<mlir::cir::FuncOp>(Op);
if (!Fn || Fn.getFunctionType() != Ty)
Fn = GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/true,
ForDefinition);

// Already emitted.
if (!Fn.isDeclaration())
return;
}
auto Fn = cast<mlir::cir::FuncOp>(Op);

setFunctionLinkage(GD, Fn);
setGVProperties(Op, D);
setGVProperties(Fn, D);
// TODO(cir): MaubeHandleStaticInExternC
// TODO(cir): maybeSetTrivialComdat
// TODO(cir): setLLVMFunctionFEnvAttributes
Expand All @@ -633,7 +630,7 @@ void CIRGenModule::buildGlobalFunctionDefinition(GlobalDecl GD,
}
CurCGF = nullptr;

setNonAliasAttributes(GD, Op);
setNonAliasAttributes(GD, Fn);
setCIRFunctionAttributesForDefinition(D, Fn);

if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
Expand Down Expand Up @@ -2670,7 +2667,8 @@ mlir::cir::FuncOp CIRGenModule::GetOrCreateCIRFunction(
// CHeck that GD is not yet in DiagnosedConflictingDefinitions is required
// to make sure that we issue and error only once.
if (lookupRepresentativeDecl(MangledName, OtherGD) &&
(GD.getCanonicalDecl().getDecl()) &&
(GD.getCanonicalDecl().getDecl() !=
OtherGD.getCanonicalDecl().getDecl()) &&
DiagnosedConflictingDefinitions.insert(GD).second) {
getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
<< MangledName;
Expand Down
15 changes: 15 additions & 0 deletions clang/test/CIR/CodeGen/same-mangled-name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s

// This would previously emit a "definition with same mangled name as another
// definition" error: https://github.com/llvm/clangir/issues/991.
namespace N {
struct S {
// CHECK: cir.func linkonce_odr @_ZN1N1S3fooEv({{.*}} {
void foo() {}
};

// CHECK: cir.func @_ZN1N1fEv() {{.*}} {
// CHECK: cir.call @_ZN1N1S3fooEv(
void f() { S().foo(); }
} // namespace N