[PATCH] D99482: [PoC][Clang][CodeGen] Do not use getelementptr for scalable struct.

Hsiangkai Wang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 28 21:17:35 PDT 2021


HsiangKai created this revision.
HsiangKai added reviewers: craig.topper, efriedma, c-rhodes, sdesmalen, rogfer01, frasercrmck, david-arm.
Herald added a subscriber: StephenFan.
HsiangKai requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

We do not support getelementptr for scalable struct. Avoid using
getelementptr when dealing with scalable struct arguments.

Use multiple insertvalue and store the whole scalable struct at the end.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99482

Files:
  clang/lib/CodeGen/CGCall.cpp


Index: clang/lib/CodeGen/CGCall.cpp
===================================================================
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -1298,10 +1298,14 @@
                                          bool DestIsVolatile) {
   // Prefer scalar stores to first-class aggregate stores.
   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(Val->getType())) {
-    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
-      Address EltPtr = Builder.CreateStructGEP(Dest, i);
-      llvm::Value *Elt = Builder.CreateExtractValue(Val, i);
-      Builder.CreateStore(Elt, EltPtr, DestIsVolatile);
+    if (STy->containsScalableVectorType())
+      Builder.CreateStore(Val, Dest, DestIsVolatile);
+    else {
+      for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
+        Address EltPtr = Builder.CreateStructGEP(Dest, i);
+        llvm::Value *Elt = Builder.CreateExtractValue(Val, i);
+        Builder.CreateStore(Elt, EltPtr, DestIsVolatile);
+      }
     }
   } else {
     Builder.CreateStore(Val, Dest, DestIsVolatile);
@@ -2857,11 +2861,21 @@
         }
 
         assert(STy->getNumElements() == NumIRArgs);
-        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
-          auto AI = Fn->getArg(FirstIRArg + i);
-          AI->setName(Arg->getName() + ".coerce" + Twine(i));
-          Address EltPtr = Builder.CreateStructGEP(AddrToStoreInto, i);
-          Builder.CreateStore(AI, EltPtr);
+        if (STy->containsScalableVectorType()) {
+          llvm::Value *Val = llvm::UndefValue::get(STy);
+          for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
+            auto AI = Fn->getArg(FirstIRArg + i);
+            AI->setName(Arg->getName() + ".coerce" + Twine(i));
+            Val = Builder.CreateInsertValue(Val, AI, i);
+          }
+          Builder.CreateStore(Val, AddrToStoreInto);
+        } else {
+          for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
+            auto AI = Fn->getArg(FirstIRArg + i);
+            AI->setName(Arg->getName() + ".coerce" + Twine(i));
+            Address EltPtr = Builder.CreateStructGEP(AddrToStoreInto, i);
+            Builder.CreateStore(AI, EltPtr);
+          }
         }
 
         if (SrcSize > DstSize) {
@@ -4920,10 +4934,18 @@
         }
 
         assert(NumIRArgs == STy->getNumElements());
-        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
-          Address EltPtr = Builder.CreateStructGEP(Src, i);
-          llvm::Value *LI = Builder.CreateLoad(EltPtr);
-          IRCallArgs[FirstIRArg + i] = LI;
+        if (STy->containsScalableVectorType()) {
+          llvm::Value *Val = Builder.CreateLoad(Src);
+          for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
+            llvm::Value *LI = Builder.CreateExtractValue(Val, i);
+            IRCallArgs[FirstIRArg + i] = LI;
+          }
+        } else {
+          for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
+            Address EltPtr = Builder.CreateStructGEP(Src, i);
+            llvm::Value *LI = Builder.CreateLoad(EltPtr);
+            IRCallArgs[FirstIRArg + i] = LI;
+          }
         }
       } else {
         // In the simple case, just pass the coerced loaded value.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99482.333761.patch
Type: text/x-patch
Size: 3286 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210329/929db3b3/attachment.bin>


More information about the cfe-commits mailing list