[llvm] 321cbf7 - [Verifier] Verify parameter alignment.

via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 26 17:46:54 PDT 2022


Author: Luo, Yuanke
Date: 2022-03-27T08:35:05+08:00
New Revision: 321cbf75be2c0f4f33856f4b1246f09800cc91d0

URL: https://github.com/llvm/llvm-project/commit/321cbf75be2c0f4f33856f4b1246f09800cc91d0
DIFF: https://github.com/llvm/llvm-project/commit/321cbf75be2c0f4f33856f4b1246f09800cc91d0.diff

LOG: [Verifier] Verify parameter alignment.

In DAGISel, the parameter alignment only have 4 bits to hold the value.
The encode(alignment) would plus the shift value by 1, so the max aligment
ISel can support is 2^14. This patch verify the parameter and return
value for alignment.

Differential Revision: https://reviews.llvm.org/D121898

Added: 
    llvm/test/Verifier/param-align.ll
    llvm/test/Verifier/param-ret-align.ll

Modified: 
    llvm/include/llvm/CodeGen/TargetCallingConv.h
    llvm/lib/IR/Verifier.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/TargetCallingConv.h b/llvm/include/llvm/CodeGen/TargetCallingConv.h
index 62365330379d8..1333f2d989731 100644
--- a/llvm/include/llvm/CodeGen/TargetCallingConv.h
+++ b/llvm/include/llvm/CodeGen/TargetCallingConv.h
@@ -46,7 +46,8 @@ namespace ISD {
     unsigned IsHvaStart : 1;   ///< HVA structure start
     unsigned IsSecArgPass : 1; ///< Second argument
     unsigned MemAlign : 4;     ///< Log 2 of alignment when arg is passed in memory
-                               ///< (including byval/byref)
+                               ///< (including byval/byref). The max alignment is
+                               ///< verified in IR verification.
     unsigned OrigAlign : 5;    ///< Log 2 of original alignment
     unsigned IsInConsecutiveRegsLast : 1;
     unsigned IsInConsecutiveRegs : 1;

diff  --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index ec15f5f1efae2..94f7ccc4b6785 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -279,6 +279,12 @@ namespace {
 class Verifier : public InstVisitor<Verifier>, VerifierSupport {
   friend class InstVisitor<Verifier>;
 
+  // ISD::ArgFlagsTy::MemAlign only have 4 bits for alignment, so
+  // the alignment size should not exceed 2^15. Since encode(Align)
+  // would plus the shift value by 1, the alignment size should
+  // not exceed 2^14, otherwise it can NOT be properly lowered
+  // in backend.
+  static constexpr unsigned ParamMaxAlignment = 1 << 14;
   DominatorTree DT;
 
   /// When verifying a basic block, keep track of all of the
@@ -3143,6 +3149,21 @@ void Verifier::visitCallBase(CallBase &Call) {
   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);
+    Align MaxAlign(ParamMaxAlignment);
+    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();

diff  --git a/llvm/test/Verifier/param-align.ll b/llvm/test/Verifier/param-align.ll
new file mode 100644
index 0000000000000..7217e47004d4a
--- /dev/null
+++ b/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>)

diff  --git a/llvm/test/Verifier/param-ret-align.ll b/llvm/test/Verifier/param-ret-align.ll
new file mode 100644
index 0000000000000..d55165685fbce
--- /dev/null
+++ b/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()


        


More information about the llvm-commits mailing list