[llvm] 40078a6 - AMDGPU: Use BinaryByteStream in printf expansion

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 6 14:22:19 PST 2023


Author: Matt Arsenault
Date: 2023-01-06T17:22:13-05:00
New Revision: 40078a6b713730ffc164d4c0733d26352eb1e236

URL: https://github.com/llvm/llvm-project/commit/40078a6b713730ffc164d4c0733d26352eb1e236
DIFF: https://github.com/llvm/llvm-project/commit/40078a6b713730ffc164d4c0733d26352eb1e236.diff

LOG: AMDGPU: Use BinaryByteStream in printf expansion

Attempt to fix test failures on big endian bots. This pass definitely
needs more test coverage.

Added: 
    

Modified: 
    llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp b/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp
index f028e190db4b2..5e7587f1ea507 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp
@@ -27,6 +27,7 @@
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/InitializePasses.h"
+#include "llvm/Support/BinaryByteStream.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 
 using namespace llvm;
@@ -149,8 +150,8 @@ bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
   IRBuilder<> Builder(Ctx);
   Type *I32Ty = Type::getInt32Ty(Ctx);
   unsigned UniqID = 0;
-  // NB: This is important for this string size to be divisible by 4
-  const char NonLiteralStr[4] = "???";
+  constexpr StringLiteral NonLiteralStr("???");
+  static_assert(NonLiteralStr.size() == 3);
 
   for (auto *CI : Printfs) {
     unsigned NumOps = CI->arg_size();
@@ -250,6 +251,7 @@ bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
         }
       }
       if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
+        ArgSize = NonLiteralStr.size() + 1;
         if (auto *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
           auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
           if (GV && GV->hasInitializer()) {
@@ -270,11 +272,7 @@ bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
               }
               ArgSize = NSizeStr;
             }
-          } else {
-            ArgSize = sizeof(NonLiteralStr);
           }
-        } else {
-          ArgSize = sizeof(NonLiteralStr);
         }
       }
       LLVM_DEBUG(dbgs() << "Printf ArgSize (in buffer) = " << ArgSize
@@ -420,7 +418,7 @@ bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
         WhatToStore.push_back(Arg);
       } else if (ArgType->getTypeID() == Type::PointerTyID) {
         if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
-          const char *S = NonLiteralStr;
+          StringRef S = NonLiteralStr;
           if (auto *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
             auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
             if (GV && GV->hasInitializer()) {
@@ -428,31 +426,34 @@ bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
               bool IsZeroValue = Init->isZeroValue();
               auto *CA = dyn_cast<ConstantDataArray>(Init);
               if (IsZeroValue || (CA && CA->isString())) {
-                S = IsZeroValue ? "" : CA->getAsCString().data();
+                S = IsZeroValue ? "" : CA->getAsCString();
               }
             }
           }
-          size_t SizeStr = strlen(S) + 1;
-          size_t Rem = SizeStr % DWORD_ALIGN;
-          size_t NSizeStr = 0;
-          if (Rem) {
-            NSizeStr = SizeStr + (DWORD_ALIGN - Rem);
-          } else {
-            NSizeStr = SizeStr;
-          }
-          if (S[0]) {
-            char *MyNewStr = new char[NSizeStr]();
-            strcpy(MyNewStr, S);
-            int NumInts = NSizeStr / 4;
-            int CharC = 0;
-            while (NumInts) {
-              int ANum = *(int *)(MyNewStr + CharC);
-              CharC += 4;
-              NumInts--;
-              Value *ANumV = ConstantInt::get(Int32Ty, ANum, false);
-              WhatToStore.push_back(ANumV);
+
+          if (!S.empty()) {
+            const size_t NSizeStr = S.size() + 1;
+            const size_t ReadSize = 4;
+
+            BinaryByteStream Streamer(S, support::little);
+
+            for (uint64_t Offset = 0; Offset < NSizeStr; Offset += ReadSize) {
+              ArrayRef<uint8_t> ReadBytes;
+              if (Error Err = Streamer.readBytes(
+                      Offset, std::min(ReadSize, S.size() - Offset), ReadBytes))
+                cantFail(std::move(Err),
+                         "failed to read bytes from constant array");
+
+              APInt IntVal(8 * ReadBytes.size(), 0);
+              LoadIntFromMemory(IntVal, ReadBytes.data(), ReadBytes.size());
+
+              // TODO: Should not bothering aligning up.
+              if (ReadBytes.size() < ReadSize)
+                IntVal = IntVal.zext(8 * ReadSize);
+
+              Type *IntTy = Type::getIntNTy(Ctx, IntVal.getBitWidth());
+              WhatToStore.push_back(ConstantInt::get(IntTy, IntVal));
             }
-            delete[] MyNewStr;
           } else {
             // Empty string, give a hint to RT it is no NULL
             Value *ANumV = ConstantInt::get(Int32Ty, 0xFFFFFF00, false);


        


More information about the llvm-commits mailing list