[PATCH] D158353: WIP: [mem2reg] Refactor load of uninitialized memory to poison semantics

John McIver via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 21 10:06:39 PDT 2023


jmciver created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
jmciver retitled this revision from "[mem2reg] Refactor load of uninitialized memory to poison semantics" to "WIP: [mem2reg] Refactor load of uninitialized memory to poison semantics".
jmciver updated this revision to Diff 551801.
jmciver added a comment.
jmciver added a subscriber: nlopes.
jmciver edited the summary of this revision.
jmciver edited the summary of this revision.
jmciver added reviewers: nikic, efriedma, fhahn.
Herald added a subscriber: StephenFan.
jmciver published this revision for review.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Remove extra call to replaceAllUsesWith.


Part of a sequence of exploratory patches using poison semantics for
uninitialized memory.

This commit adds freeze_bits metadata support to mem2reg.

Regression test changes will be added in a future WIP patch.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158353

Files:
  llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp


Index: llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
===================================================================
--- llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
+++ llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
@@ -39,6 +39,7 @@
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Intrinsics.h"
+#include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Type.h"
@@ -599,17 +600,37 @@
         less_first());
     Value *ReplVal;
     if (I == StoresByIndex.begin()) {
-      if (StoresByIndex.empty())
-        // If there are no stores, the load takes the undef value.
-        ReplVal = getInitialValueOfAllocation(AI, nullptr, LI->getType()).second;
-      else
+      if (StoresByIndex.empty()) {
+        // If there are no stores, the load may take a poison or freeze poison
+        // value value.
+        auto [QueryEnum, QueryReplVal] =
+            getInitialValueOfAllocation(AI, nullptr, LI->getType(), LI);
+        switch (QueryEnum) {
+        case InitializationCategory::Unknown:
+          return false;
+          break;
+        case InitializationCategory::Constant:
+          ReplVal = QueryReplVal;
+          break;
+        case InitializationCategory::FreezePoison:
+          IRBuilder<> Builder(LI);
+          ReplVal =
+              Builder.CreateFreeze(PoisonValue::get(LI->getType()), "freeze");
+          break;
+        }
+      } else {
         // There is no store before this load, bail out (load may be affected
         // by the following stores - see main comment).
         return false;
+      }
     } else {
       // Otherwise, there was a store before this load, the load takes its
       // value.
       ReplVal = std::prev(I)->second->getOperand(0);
+      if (loadHasFreezeBits(LI)) {
+        IRBuilder<> Builder(LI);
+        ReplVal = Builder.CreateFreeze(ReplVal, "freeze");
+      }
     }
 
     convertMetadataToAssumes(LI, ReplVal, DL, AC, &DT);
@@ -762,7 +783,7 @@
   // been stored yet.  In this case, it will get this null value.
   RenamePassData::ValVector Values(Allocas.size());
   for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
-    Values[i] = UndefValue::get(Allocas[i]->getAllocatedType());
+    Values[i] = PoisonValue::get(Allocas[i]->getAllocatedType());
 
   // When handling debug info, treat all incoming values as if they have unknown
   // locations until proven otherwise.
@@ -1078,6 +1099,10 @@
       convertMetadataToAssumes(LI, V, SQ.DL, AC, &DT);
 
       // Anything using the load now uses the current value.
+      if (loadHasFreezeBits(LI)) {
+        IRBuilder<> Builder(LI);
+        V = Builder.CreateFreeze(V, "freeze.load");
+      }
       LI->replaceAllUsesWith(V);
       LI->eraseFromParent();
     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158353.551801.patch
Type: text/x-patch
Size: 2889 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230821/7bd5c6b0/attachment.bin>


More information about the llvm-commits mailing list