Skip to content

[AsmParser] Add missing globals declarations in incomplete IR mode #79855

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 1 commit into from
Jan 31, 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
44 changes: 32 additions & 12 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,7 @@ bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {
"use of undefined comdat '$" +
ForwardRefComdats.begin()->first + "'");

// Automatically create declarations for intrinsics. Intrinsics can only be
// called directly, so the call function type directly determines the
// declaration function type.
for (const auto &[Name, Info] : make_early_inc_range(ForwardRefVals)) {
if (!StringRef(Name).starts_with("llvm."))
continue;

// Don't do anything if the intrinsic is called with different function
// types. This would result in a verifier error anyway.
auto GetCommonFunctionType = [](Value *V) -> FunctionType * {
FunctionType *FTy = nullptr;
for (User *U : V->users()) {
Expand All @@ -322,10 +314,38 @@ bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {
}
return FTy;
};
if (FunctionType *FTy = GetCommonFunctionType(Info.first)) {
Function *Fn =
Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
Info.first->replaceAllUsesWith(Fn);

auto GetDeclarationType = [&](StringRef Name, Value *V) -> Type * {
// Automatically create declarations for intrinsics. Intrinsics can only
// be called directly, so the call function type directly determines the
// declaration function type.
if (Name.starts_with("llvm."))
// Don't do anything if the intrinsic is called with different function
// types. This would result in a verifier error anyway.
return GetCommonFunctionType(V);

if (AllowIncompleteIR) {
// If incomplete IR is allowed, also add declarations for
// non-intrinsics. First check whether this global is only used in
// calls with the same type, in which case we'll insert a function.
if (auto *Ty = GetCommonFunctionType(V))
return Ty;

// Otherwise, fall back to using a dummy i8 type.
return Type::getInt8Ty(Context);
}
return nullptr;
};

if (Type *Ty = GetDeclarationType(Name, Info.first)) {
GlobalValue *GV;
if (auto *FTy = dyn_cast<FunctionType>(Ty))
GV = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
else
GV = new GlobalVariable(*M, Ty, /*isConstant*/ false,
GlobalValue::ExternalLinkage,
/*Initializer*/ nullptr, Name);
Info.first->replaceAllUsesWith(GV);
Info.first->eraseFromParent();
ForwardRefVals.erase(Name);
}
Expand Down
20 changes: 20 additions & 0 deletions llvm/test/Assembler/incomplete-ir-declarations.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; RUN: opt -S -allow-incomplete-ir < %s | FileCheck %s

; CHECK: @fn2 = external global i8
; CHECK: @g1 = external global i8
; CHECK: @g2 = external global i8
; CHECK: @g3 = external global i8

; CHECK: declare void @fn1(i32)

define ptr @test() {
call void @fn1(i32 0)
call void @fn1(i32 1)
call void @fn2(i32 2)
call void @fn2(i32 2, i32 3)
load i32, ptr @g1
store i32 0, ptr @g1
load i32, ptr @g1
load i64, ptr @g2
ret ptr @g3
}