[PATCH] D121898: [Verifier] Verify parameter alignment.
LuoYuanke via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 17 03:24:42 PDT 2022
LuoYuanke created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
LuoYuanke requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
In DAGISel, the parameter alignment only have 4 bits to hold the value.
The encode(alignment) would need extra 1 bit, so the max aligment that
ISel can support is 2^14. This patch verify the parameter, return value
and align attribute for parameter.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D121898
Files:
llvm/lib/IR/Verifier.cpp
llvm/test/Verifier/param-align.ll
llvm/test/Verifier/param-attr-align.ll
llvm/test/Verifier/param-ret-align.ll
Index: llvm/test/Verifier/param-ret-align.ll
===================================================================
--- /dev/null
+++ llvm/test/Verifier/param-ret-align.ll
@@ -0,0 +1,10 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+
+; CHECK: Incorrect alignment of return type to called function!
+define dso_local void @foo() {
+entry:
+ call <8192 x float> @bar()
+ ret void
+}
+
+declare dso_local <8192 x float> @bar()
Index: llvm/test/Verifier/param-attr-align.ll
===================================================================
--- /dev/null
+++ llvm/test/Verifier/param-attr-align.ll
@@ -0,0 +1,11 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+
+; CHECK: Attribute 'align 32768' exceed the max size 2^14
+define dso_local void @foo(i8* %p) {
+entry:
+ %p1 = bitcast i8* %p to <8 x float>*
+ call void @bar(<8 x float>* noundef byval(<8 x float>) align 32768 %p1)
+ ret void
+}
+
+declare dso_local void @bar(<8 x float>* %p)
Index: llvm/test/Verifier/param-align.ll
===================================================================
--- /dev/null
+++ llvm/test/Verifier/param-align.ll
@@ -0,0 +1,10 @@
+; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+
+; CHECK: Incorrect alignment of argument passed to called function!
+define dso_local void @foo(<8192 x float> noundef %vec) {
+entry:
+ call void @bar(<8192 x float> %vec)
+ ret void
+}
+
+declare dso_local void @bar(<8192 x float>)
Index: llvm/lib/IR/Verifier.cpp
===================================================================
--- llvm/lib/IR/Verifier.cpp
+++ llvm/lib/IR/Verifier.cpp
@@ -1731,12 +1731,23 @@
verifyAttributeTypes(Attrs, V);
- for (Attribute Attr : Attrs)
+ for (Attribute Attr : Attrs) {
Assert(Attr.isStringAttribute() ||
Attribute::canUseAsParamAttr(Attr.getKindAsEnum()),
"Attribute '" + Attr.getAsString() +
"' does not apply to parameters",
V);
+ if (Attr.getKindAsEnum() == Attribute::Alignment) {
+ Align AttrAlign = Attr.getAlignment().valueOrOne();
+ // ArgFlagsTy::MemAlign only have 4 bits for alignment, so the
+ // alignment size should not exceed 1 << 14, otherwise it can NOT
+ // be properly lowered.
+ Align MaxAlign(1 << 15);
+ Assert(AttrAlign < MaxAlign,
+ "Attribute '" + Attr.getAsString() + "' exceed the max size 2^14",
+ V);
+ }
+ }
if (Attrs.hasAttribute(Attribute::ImmArg)) {
Assert(Attrs.getNumAttributes() == 1,
@@ -3142,6 +3153,24 @@
Assert(verifyAttributeCount(Attrs, Call.arg_size()),
"Attribute after last parameter!", Call);
+ auto VerifyTypeAlign = [&](Type *Ty, const Twine &Message) {
+ if (!Ty->isSized())
+ return;
+ Align ABIAlign = DL.getABITypeAlign(Ty);
+ // ArgFlagsTy::MemAlign only have 4 bits for alignment, so the
+ // alignment size should not exceed 1 << 14, otherwise it can NOT
+ // be properly lowered.
+ Align MaxAlign(1 << 15);
+ Assert(ABIAlign < MaxAlign,
+ "Incorrect alignment of " + Message + " to called function!", Call);
+ };
+
+ VerifyTypeAlign(FTy->getReturnType(), "return type");
+ for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
+ Type *Ty = FTy->getParamType(i);
+ VerifyTypeAlign(Ty, "argument passed");
+ }
+
Function *Callee =
dyn_cast<Function>(Call.getCalledOperand()->stripPointerCasts());
bool IsIntrinsic = Callee && Callee->isIntrinsic();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121898.416122.patch
Type: text/x-patch
Size: 3439 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220317/90d27528/attachment.bin>
More information about the llvm-commits
mailing list