This is an archive of the discontinued LLVM Phabricator instance.

[mlir][LLVM] Verify correct pointer casts with `llvm.bitcast`
ClosedPublic

Authored by zero9178 on Feb 12 2023, 3:04 PM.

Details

Summary

llvm.bitcast has so far not had a verifier and this allowed various bugs to sneak into the codebase (including within tests!) which could only be caught once translated to actual LLVM IR. This patch fixes those problematic cases by now verifying bitcasts on pointers are done correctly.

Specifically, it verifies that if pointers are involved, that both result and source types are pointers, that this also applies to vector of pointers and that pointer casts are of the same address space.

The only thing left unverified is the general case of "source type size does not match result type size". I think this case is less trivial and more prone to false positives, so I did not yet implement it.

Diff Detail

Event Timeline

zero9178 created this revision.Feb 12 2023, 3:04 PM
zero9178 requested review of this revision.Feb 12 2023, 3:04 PM
Dinistro added inline comments.Feb 12 2023, 11:25 PM
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2336

Should we also check if the address-spaces of pointers in vectors match up, e.g., llvm.vec<4 x ptr<1>> and llvm.vec<4 x ptr<2>>, or is that checked elsewhere already?

gysit accepted this revision.Feb 12 2023, 11:37 PM

Nice!

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2336

I believe this is checked below

resultType.getAddressSpace() != sourceType.getAddressSpace()

since extractVectorElementType gets the element type of the vector.

2353

ultra nit: I would probably put a comma.

mlir/test/Dialect/LLVMIR/invalid.mlir
1327

nit: invalid_bitcast_addrspace?

This revision is now accepted and ready to land.Feb 12 2023, 11:37 PM
zero9178 updated this revision to Diff 496890.Feb 13 2023, 2:46 AM

address review comments and add test to demonstrate invalid address casts on vector of pointers

zero9178 marked 4 inline comments as done.Feb 13 2023, 2:46 AM
zero9178 added inline comments.
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2336

I've added a test to show it works with vectors too

Dinistro accepted this revision.Feb 13 2023, 6:55 AM

LGTM!

This revision was automatically updated to reflect the committed changes.
zero9178 marked an inline comment as done.