Skip to content

Commit

Permalink
[Verifier] Check that !nonnull metadata is empty
Browse files Browse the repository at this point in the history
!nonnull expectes an empty metadata argument, so check that this
is the case in the verifier. This came up as a problem in
https://reviews.llvm.org/D141386.

This requires dropping the verifier call in the compatibility-6.0.ll
test (which is not present in any of the other bitcode compatibility
tests). The original input unfortunately used typo'd nonnull
metadata.
  • Loading branch information
nikic committed Jan 23, 2023
1 parent 65420c8 commit 474f20b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4832,13 +4832,14 @@ void Verifier::visitInstruction(Instruction &I) {
"invariant.group metadata is only for loads and stores", &I);
}

if (I.getMetadata(LLVMContext::MD_nonnull)) {
if (MDNode *MD = I.getMetadata(LLVMContext::MD_nonnull)) {
Check(I.getType()->isPointerTy(), "nonnull applies only to pointer types",
&I);
Check(isa<LoadInst>(I),
"nonnull applies only to load instructions, use attributes"
" for calls or invokes",
&I);
Check(MD->getNumOperands() == 0, "nonnull metadata must be empty", &I);
}

if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable))
Expand Down
1 change: 0 additions & 1 deletion llvm/test/Bitcode/compatibility-6.0.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
; N.b: This is 6.0-compatible IR. The CHECK lines occasionally differ from
; the IR used to generate the bitcode, and may need to be updated.

; RUN: opt -passes=verify -disable-output < %s.bc
; RUN: llvm-dis < %s.bc | FileCheck %s

target datalayout = "E"
Expand Down
21 changes: 21 additions & 0 deletions llvm/test/Verifier/nonnull_metadata.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; RUN: not llvm-as < %s 2>&1 | FileCheck %s

declare ptr @dummy()

; CHECK: nonnull applies only to pointer types
define void @test_not_pointer(ptr %p) {
load i32, ptr %p, !nonnull !{}
ret void
}

; CHECK: nonnull applies only to load instructions, use attributes for calls or invokes
define void @test_not_load() {
call ptr @dummy(), !nonnull !{}
ret void
}

; CHECK: nonnull metadata must be empty
define void @test_invalid_arg(ptr %p) {
load ptr, ptr %p, !nonnull !{i32 0}
ret void
}

0 comments on commit 474f20b

Please sign in to comment.