Skip to content

Commit 8b98612

Browse files
authored
[CIR] Fix calling defined functions (#137271)
Until now our function symbol lookup has been assuming that the function did not exist and creating a definition for it. This caused us to create a duplicate definition if we ever tried to call a function that was already defined. This change fixes that by adding handling for trying to look up existing global definitions before creating a new one.
1 parent 77f8335 commit 8b98612

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ struct MissingFeatures {
160160
static bool lambdaFieldToName() { return false; }
161161
static bool targetSpecificCXXABI() { return false; }
162162
static bool moduleNameHash() { return false; }
163+
static bool setDSOLocal() { return false; }
163164

164165
// Missing types
165166
static bool dataMemberType() { return false; }

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,58 @@ cir::FuncOp CIRGenModule::getOrCreateCIRFunction(
773773
StringRef mangledName, mlir::Type funcType, GlobalDecl gd, bool forVTable,
774774
bool dontDefer, bool isThunk, ForDefinition_t isForDefinition,
775775
mlir::ArrayAttr extraAttrs) {
776+
const Decl *d = gd.getDecl();
777+
778+
if (isThunk)
779+
errorNYI(d->getSourceRange(), "getOrCreateCIRFunction: thunk");
780+
781+
// In what follows, we continue past 'errorNYI' as if nothing happened because
782+
// the rest of the implementation is better than doing nothing.
783+
784+
if (const auto *fd = cast_or_null<FunctionDecl>(d)) {
785+
// For the device mark the function as one that should be emitted.
786+
if (getLangOpts().OpenMPIsTargetDevice && fd->isDefined() && !dontDefer &&
787+
!isForDefinition)
788+
errorNYI(fd->getSourceRange(),
789+
"getOrCreateCIRFunction: OpenMP target function");
790+
791+
// Any attempts to use a MultiVersion function should result in retrieving
792+
// the iFunc instead. Name mangling will handle the rest of the changes.
793+
if (fd->isMultiVersion())
794+
errorNYI(fd->getSourceRange(), "getOrCreateCIRFunction: multi-version");
795+
}
796+
797+
// Lookup the entry, lazily creating it if necessary.
798+
mlir::Operation *entry = getGlobalValue(mangledName);
799+
if (entry) {
800+
if (!isa<cir::FuncOp>(entry))
801+
errorNYI(d->getSourceRange(), "getOrCreateCIRFunction: non-FuncOp");
802+
803+
assert(!cir::MissingFeatures::weakRefReference());
804+
805+
// Handle dropped DLL attributes.
806+
if (d && !d->hasAttr<DLLImportAttr>() && !d->hasAttr<DLLExportAttr>()) {
807+
assert(!cir::MissingFeatures::setDLLStorageClass());
808+
assert(!cir::MissingFeatures::setDSOLocal());
809+
}
810+
811+
// If there are two attempts to define the same mangled name, issue an
812+
// error.
813+
auto fn = cast<cir::FuncOp>(entry);
814+
assert((!isForDefinition || !fn || !fn.isDeclaration()) &&
815+
"Duplicate function definition");
816+
if (fn && fn.getFunctionType() == funcType) {
817+
return fn;
818+
}
819+
820+
if (!isForDefinition) {
821+
return fn;
822+
}
823+
824+
// TODO(cir): classic codegen checks here if this is a llvm::GlobalAlias.
825+
// How will we support this?
826+
}
827+
776828
auto *funcDecl = llvm::cast_or_null<FunctionDecl>(gd.getDecl());
777829
bool invalidLoc = !funcDecl ||
778830
funcDecl->getSourceRange().getBegin().isInvalid() ||

clang/test/CIR/CodeGen/call.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - 2>&1 | FileCheck %s
22

3-
void f1();
3+
void f1() {}
44
void f2() {
55
f1();
66
}
77

88
// CHECK-LABEL: cir.func @_Z2f1v
9+
// CHECK-LABEL: cir.func @_Z2f2v
910
// CHECK: cir.call @_Z2f1v() : () -> ()
1011

11-
int f3();
12+
int f3() { return 2; }
1213
int f4() {
1314
int x = f3();
1415
return x;
1516
}
1617

18+
// CHECK-LABEL: cir.func @_Z2f3v() -> !s32i
1719
// CHECK-LABEL: cir.func @_Z2f4v() -> !s32i
18-
// CHECK: %[[#x:]] = cir.call @_Z2f3v() : () -> !s32i
19-
// CHECK-NEXT: cir.store %[[#x]], %{{.+}} : !s32i, !cir.ptr<!s32i>
20+
// CHECK: cir.call @_Z2f3v() : () -> !s32i

clang/test/CIR/CodeGen/namespace.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
namespace {
55
int g1 = 1;
66

7-
// Note: This causes a warning about the function being undefined, but we
8-
// currently have a problem with duplicate definitions when we call functions.
9-
// This should be updated when that problem is fixed.
10-
void f1(void);
7+
void f1(void) {}
118
}
129

1310

0 commit comments

Comments
 (0)