Skip to content

Commit 5d57647

Browse files
committed
[IRGen] Detect duplicate definitions at the IR level
1 parent 523b3b7 commit 5d57647

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

include/swift/AST/DiagnosticsIRGen.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,9 @@ NOTE(maybe_missing_parameter, none,
117117
"%select{constraint|parameter}0",
118118
(bool, const clang::NamedDecl *))
119119

120+
ERROR(ir_function_redefinition_external,none,
121+
"multiple definitions of symbol '%0'",
122+
(StringRef))
123+
120124
#define UNDEFINE_DIAGNOSTIC_MACROS
121125
#include "DefineDiagnosticMacros.h"

lib/IRGen/IRGenSIL.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ class IRGenSILFunction :
451451
llvm::DenseMap<SILInstruction *, llvm::SmallVector<StackPackAlloc, 2>>
452452
StackPackAllocs;
453453

454-
IRGenSILFunction(IRGenModule &IGM, SILFunction *f);
454+
IRGenSILFunction(IRGenModule &IGM, SILFunction *f, llvm::Function *llvmF);
455455
~IRGenSILFunction();
456456

457457
/// Generate IR for the SIL Function.
@@ -1887,10 +1887,9 @@ llvm::Value *LoweredValue::getSingletonExplosion(IRGenFunction &IGF,
18871887
llvm_unreachable("bad kind");
18881888
}
18891889

1890-
IRGenSILFunction::IRGenSILFunction(IRGenModule &IGM, SILFunction *f)
1891-
: IRGenFunction(IGM,
1892-
IGM.getAddrOfSILFunction(f, ForDefinition,
1893-
f->isDynamicallyReplaceable()),
1890+
IRGenSILFunction::IRGenSILFunction(IRGenModule &IGM, SILFunction *f,
1891+
llvm::Function *llvmF)
1892+
: IRGenFunction(IGM, llvmF,
18941893
f->isPerformanceConstraint(),
18951894
f->getOptimizationMode(), f->getDebugScope(),
18961895
f->getLocation()),
@@ -2558,7 +2557,17 @@ void IRGenModule::emitSILFunction(SILFunction *f) {
25582557
noteUseOfMetadataByCXXInterop(IRGen, f, TypeExpansionContext(*f));
25592558

25602559
PrettyStackTraceSILFunction stackTrace("emitting IR", f);
2561-
IRGenSILFunction(*this, f).emitSILFunction();
2560+
2561+
// Get the LLVM function we will emit. If it has already been defined, error.
2562+
auto llvmF = getAddrOfSILFunction(f, ForDefinition,
2563+
f->isDynamicallyReplaceable());
2564+
if (!llvmF->empty()) {
2565+
auto &diags = Context.Diags;
2566+
diags.diagnose(f->getLocation().getSourceLoc(), diag::ir_function_redefinition_external, llvmF->getName());
2567+
return;
2568+
}
2569+
2570+
IRGenSILFunction(*this, f, llvmF).emitSILFunction();
25622571
}
25632572

25642573
void IRGenSILFunction::emitSILFunction() {

lib/SIL/IR/SILFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ bool SILFunction::hasForeignBody() const {
443443
}
444444

445445
void SILFunction::setAsmName(StringRef value) {
446-
assert((AsmName.empty() || value == AsmName) && "Cannot change asmname");
446+
ASSERT((AsmName.empty() || value == AsmName) && "Cannot change asmname");
447447
AsmName = value;
448448

449449
if (!value.empty()) {

lib/SIL/IR/SILGlobalVariable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ SILGlobalVariable::~SILGlobalVariable() {
8585
}
8686

8787
void SILGlobalVariable::setAsmName(StringRef value) {
88-
assert((AsmName.empty() || value == AsmName) && "Cannot change asmname");
88+
ASSERT((AsmName.empty() || value == AsmName) && "Cannot change asmname");
8989
AsmName = value;
9090

9191
if (!value.empty()) {

lib/Serialization/DeserializeSIL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4148,7 +4148,7 @@ SILGlobalVariable *SILDeserializer::lookupSILGlobalVariable(StringRef name,
41484148

41494149
SILGlobalVariable *SILDeserializer::readGlobalVar(StringRef Name,
41504150
bool byAsmName) {
4151-
// If we're looking up the function by its AsmName, check that table.
4151+
// If we're looking up the variable by its AsmName, check that table.
41524152
if (byAsmName) {
41534153
if (!AsmNameTable)
41544154
return nullptr;

0 commit comments

Comments
 (0)