Skip to content

Commit 34b1067

Browse files
authored
[AsmParser] Implicitly declare intrinsics (#78251)
We currently require that all referenced globals have an explicit declaration or definition in the IR. For intrinsics, this requirement is redundant, because they cannot be called indirectly (including "direct" calls with mismatched function type). The function type used in the call directly determines the function type of the intrinsic declaration. Relax this requirement, and implicitly declare any intrinsics that do not have an explicit declaration. This will remove a common annoyance when writing tests and alive2 proofs. (I also plan to introduce a mode where declarations for all missing symbols will be automatically added, to make working with incomplete IR easier -- but that will be behind a default-disabled flag.)
1 parent 435bcea commit 34b1067

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,34 @@ bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {
246246
"use of undefined comdat '$" +
247247
ForwardRefComdats.begin()->first + "'");
248248

249+
// Automatically create declarations for intrinsics. Intrinsics can only be
250+
// called directly, so the call function type directly determines the
251+
// declaration function type.
252+
for (const auto &[Name, Info] : make_early_inc_range(ForwardRefVals)) {
253+
if (!StringRef(Name).starts_with("llvm."))
254+
continue;
255+
256+
// Don't do anything if the intrinsic is called with different function
257+
// types. This would result in a verifier error anyway.
258+
auto GetCommonFunctionType = [](Value *V) -> FunctionType * {
259+
FunctionType *FTy = nullptr;
260+
for (User *U : V->users()) {
261+
auto *CB = dyn_cast<CallBase>(U);
262+
if (!CB || (FTy && FTy != CB->getFunctionType()))
263+
return nullptr;
264+
FTy = CB->getFunctionType();
265+
}
266+
return FTy;
267+
};
268+
if (FunctionType *FTy = GetCommonFunctionType(Info.first)) {
269+
Function *Fn =
270+
Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
271+
Info.first->replaceAllUsesWith(Fn);
272+
Info.first->eraseFromParent();
273+
ForwardRefVals.erase(Name);
274+
}
275+
}
276+
249277
if (!ForwardRefVals.empty())
250278
return error(ForwardRefVals.begin()->second.second,
251279
"use of undefined value '@" + ForwardRefVals.begin()->first +
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; RUN: not llvm-as < %s 2>&1 | FileCheck %s
2+
3+
; Check that intrinsics do not get automatically declared if they are used
4+
; with different function types.
5+
6+
; CHECK: error: use of undefined value '@llvm.umax'
7+
define void @test() {
8+
call i8 @llvm.umax(i8 0, i8 1)
9+
call i16 @llvm.umax(i16 0, i16 1)
10+
ret void
11+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2+
; RUN: opt -S < %s | FileCheck %s
3+
; RUN: opt -S -passes=instcombine < %s | FileCheck %s --check-prefix=INSTCOMBINE
4+
5+
; llvm.umax is intentionally missing the mangling suffix here, to show that
6+
; this works fine with auto-upgrade.
7+
define i16 @test(i8 %x, i8 %y) {
8+
; CHECK-LABEL: define i16 @test(
9+
; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) {
10+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[X]], -1
11+
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
12+
; CHECK-NEXT: [[MAX1:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
13+
; CHECK-NEXT: [[X_EXT:%.*]] = zext i8 [[X]] to i16
14+
; CHECK-NEXT: [[Y_EXT:%.*]] = zext i8 [[Y]] to i16
15+
; CHECK-NEXT: [[MAX2:%.*]] = call i16 @llvm.umax.i16(i16 [[X_EXT]], i16 [[Y_EXT]])
16+
; CHECK-NEXT: ret i16 [[MAX2]]
17+
;
18+
; INSTCOMBINE-LABEL: define i16 @test(
19+
; INSTCOMBINE-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) {
20+
; INSTCOMBINE-NEXT: [[CMP:%.*]] = icmp sgt i8 [[X]], -1
21+
; INSTCOMBINE-NEXT: call void @llvm.assume(i1 [[CMP]])
22+
; INSTCOMBINE-NEXT: [[TMP1:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
23+
; INSTCOMBINE-NEXT: [[MAX2:%.*]] = zext i8 [[TMP1]] to i16
24+
; INSTCOMBINE-NEXT: ret i16 [[MAX2]]
25+
;
26+
%cmp = icmp sgt i8 %x, -1
27+
call void @llvm.assume(i1 %cmp)
28+
%max1 = call i8 @llvm.umax(i8 %x, i8 %y)
29+
%x.ext = zext i8 %x to i16
30+
%y.ext = zext i8 %y to i16
31+
%max2 = call i16 @llvm.umax.i16(i16 %x.ext, i16 %y.ext)
32+
ret i16 %max2
33+
}

0 commit comments

Comments
 (0)