[llvm] b1bd64a - Revert "[NFC] [MTE] Use helpers for stack tagging."

Florian Mayer via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 11 14:41:37 PST 2022


Author: Florian Mayer
Date: 2022-02-11T14:41:24-08:00
New Revision: b1bd64aeee30773560a0cfa22db8d998aa4348d4

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

LOG: Revert "[NFC] [MTE] Use helpers for stack tagging."

This reverts commit 8f0e5b4e26a54a3ec347d6499cbb43b15ad6352c.

Added: 
    

Modified: 
    llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
    llvm/lib/Target/AArch64/AArch64StackTagging.cpp
    llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h b/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
index b466393012778..d1548ecb58c06 100644
--- a/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
+++ b/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
@@ -19,7 +19,6 @@
 #include "llvm/IR/Instruction.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Module.h"
-#include "llvm/IR/ValueHandle.h"
 
 namespace llvm {
 namespace memtag {
@@ -75,7 +74,6 @@ Instruction *getUntagLocationIfFunctionExit(Instruction &Inst);
 
 struct AllocaInfo {
   AllocaInst *AI;
-  TrackingVH<Instruction> OldAI; // Track through RAUW to replace debug uses.
   SmallVector<IntrinsicInst *, 2> LifetimeStart;
   SmallVector<IntrinsicInst *, 2> LifetimeEnd;
   SmallVector<DbgVariableIntrinsic *, 2> DbgVariableIntrinsics;

diff  --git a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
index 59037440c2b99..0336a2f1d0ee0 100644
--- a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
+++ b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
@@ -290,6 +290,14 @@ class InitializerBuilder {
 };
 
 class AArch64StackTagging : public FunctionPass {
+  struct AllocaInfo {
+    AllocaInst *AI;
+    TrackingVH<Instruction> OldAI; // Track through RAUW to replace debug uses.
+    SmallVector<IntrinsicInst *, 2> LifetimeStart;
+    SmallVector<IntrinsicInst *, 2> LifetimeEnd;
+    SmallVector<DbgVariableIntrinsic *, 2> DbgVariableIntrinsics;
+  };
+
   const bool MergeInit;
   const bool UseStackSafety;
 
@@ -305,7 +313,7 @@ class AArch64StackTagging : public FunctionPass {
   }
 
   bool isInterestingAlloca(const AllocaInst &AI);
-  void alignAndPadAlloca(memtag::AllocaInfo &Info);
+  void alignAndPadAlloca(AllocaInfo &Info);
 
   void tagAlloca(AllocaInst *AI, Instruction *InsertBefore, Value *Ptr,
                  uint64_t Size);
@@ -314,9 +322,9 @@ class AArch64StackTagging : public FunctionPass {
   Instruction *collectInitializers(Instruction *StartInst, Value *StartPtr,
                                    uint64_t Size, InitializerBuilder &IB);
 
-  Instruction *insertBaseTaggedPointer(
-      const MapVector<AllocaInst *, memtag::AllocaInfo> &Allocas,
-      const DominatorTree *DT);
+  Instruction *
+  insertBaseTaggedPointer(const MapVector<AllocaInst *, AllocaInfo> &Allocas,
+                          const DominatorTree *DT);
   bool runOnFunction(Function &F) override;
 
   StringRef getPassName() const override { return "AArch64 Stack Tagging"; }
@@ -458,12 +466,12 @@ void AArch64StackTagging::untagAlloca(AllocaInst *AI, Instruction *InsertBefore,
 }
 
 Instruction *AArch64StackTagging::insertBaseTaggedPointer(
-    const MapVector<AllocaInst *, memtag::AllocaInfo> &AllocasToInstrument,
+    const MapVector<AllocaInst *, AllocaInfo> &InterestingAllocas,
     const DominatorTree *DT) {
   BasicBlock *PrologueBB = nullptr;
   // Try sinking IRG as deep as possible to avoid hurting shrink wrap.
-  for (auto &I : AllocasToInstrument) {
-    const memtag::AllocaInfo &Info = I.second;
+  for (auto &I : InterestingAllocas) {
+    const AllocaInfo &Info = I.second;
     AllocaInst *AI = Info.AI;
     if (!PrologueBB) {
       PrologueBB = AI->getParent();
@@ -482,7 +490,7 @@ Instruction *AArch64StackTagging::insertBaseTaggedPointer(
   return Base;
 }
 
-void AArch64StackTagging::alignAndPadAlloca(memtag::AllocaInfo &Info) {
+void AArch64StackTagging::alignAndPadAlloca(AllocaInfo &Info) {
   const Align NewAlignment =
       max(MaybeAlign(Info.AI->getAlign()), kTagGranuleSize);
   Info.AI->setAlignment(NewAlignment);
@@ -528,17 +536,63 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
   if (MergeInit)
     AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
 
-  memtag::StackInfoBuilder SIB(
-      [this](const AllocaInst &AI) { return isInterestingAlloca(AI); });
-  for (Instruction &I : instructions(F))
-    SIB.visit(I);
-  memtag::StackInfo &SInfo = SIB.get();
+  MapVector<AllocaInst *, AllocaInfo>
+      InterestingAllocas; // need stable iteration order
+  SmallVector<Instruction *, 8> RetVec;
+  SmallVector<Instruction *, 4> UnrecognizedLifetimes;
+
+  bool CallsReturnTwice = false;
+  for (Instruction &I : instructions(F)) {
+    if (CallInst *CI = dyn_cast<CallInst>(&I)) {
+      if (CI->canReturnTwice()) {
+        CallsReturnTwice = true;
+      }
+    }
+    if (auto *AI = dyn_cast<AllocaInst>(&I)) {
+      if (isInterestingAlloca(*AI)) {
+        InterestingAllocas[AI].AI = AI;
+        InterestingAllocas[AI].OldAI = AI;
+      }
+      continue;
+    }
+
+    if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I)) {
+      for (Value *V : DVI->location_ops())
+        if (auto *AI = dyn_cast_or_null<AllocaInst>(V))
+          if (isInterestingAlloca(*AI) &&
+              (InterestingAllocas[AI].DbgVariableIntrinsics.empty() ||
+               InterestingAllocas[AI].DbgVariableIntrinsics.back() != DVI))
+            InterestingAllocas[AI].DbgVariableIntrinsics.push_back(DVI);
+      continue;
+    }
+
+    auto *II = dyn_cast<IntrinsicInst>(&I);
+    if (II && (II->getIntrinsicID() == Intrinsic::lifetime_start ||
+                II->getIntrinsicID() == Intrinsic::lifetime_end)) {
+      AllocaInst *AI = findAllocaForValue(II->getArgOperand(1));
+      if (!AI) {
+        UnrecognizedLifetimes.push_back(&I);
+        continue;
+      }
+      if (!isInterestingAlloca(*AI))
+        continue;
+      if (II->getIntrinsicID() == Intrinsic::lifetime_start)
+        InterestingAllocas[AI].LifetimeStart.push_back(II);
+      else
+        InterestingAllocas[AI].LifetimeEnd.push_back(II);
+      continue;
+    }
+
+    Instruction *ExitUntag = memtag::getUntagLocationIfFunctionExit(I);
+    if (ExitUntag)
+      RetVec.push_back(ExitUntag);
+  }
 
-  if (SInfo.AllocasToInstrument.empty())
+  if (InterestingAllocas.empty())
     return false;
 
-  for (auto &I : SInfo.AllocasToInstrument) {
-    memtag::AllocaInfo &Info = I.second;
+  for (auto &I : InterestingAllocas) {
+    AllocaInfo &Info = I.second;
     assert(Info.AI && isInterestingAlloca(*Info.AI));
     alignAndPadAlloca(Info);
   }
@@ -548,7 +602,7 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
   if (auto *P = getAnalysisIfAvailable<DominatorTreeWrapperPass>())
     DT = &P->getDomTree();
 
-  if (DT == nullptr && (SInfo.AllocasToInstrument.size() > 1 ||
+  if (DT == nullptr && (InterestingAllocas.size() > 1 ||
                         !F->hasFnAttribute(Attribute::OptimizeNone))) {
     DeleteDT = std::make_unique<DominatorTree>(*F);
     DT = DeleteDT.get();
@@ -567,11 +621,11 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
   SetTagFunc =
       Intrinsic::getDeclaration(F->getParent(), Intrinsic::aarch64_settag);
 
-  Instruction *Base = insertBaseTaggedPointer(SInfo.AllocasToInstrument, DT);
+  Instruction *Base = insertBaseTaggedPointer(InterestingAllocas, DT);
 
   int NextTag = 0;
-  for (auto &I : SInfo.AllocasToInstrument) {
-    const memtag::AllocaInfo &Info = I.second;
+  for (auto &I : InterestingAllocas) {
+    const AllocaInfo &Info = I.second;
     AllocaInst *AI = Info.AI;
     int Tag = NextTag;
     NextTag = (NextTag + 1) % 16;
@@ -588,15 +642,15 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
     TagPCall->setOperand(0, Info.AI);
 
     bool StandardLifetime =
-        SInfo.UnrecognizedLifetimes.empty() &&
+        UnrecognizedLifetimes.empty() &&
         memtag::isStandardLifetime(Info.LifetimeStart, Info.LifetimeEnd, DT,
                                    ClMaxLifetimes);
     // Calls to functions that may return twice (e.g. setjmp) confuse the
     // postdominator analysis, and will leave us to keep memory tagged after
     // function return. Work around this by always untagging at every return
     // statement if return_twice functions are called.
-    if (SInfo.UnrecognizedLifetimes.empty() && StandardLifetime &&
-        !SInfo.CallsReturnTwice) {
+    if (UnrecognizedLifetimes.empty() && StandardLifetime &&
+        !CallsReturnTwice) {
       IntrinsicInst *Start = Info.LifetimeStart[0];
       uint64_t Size =
           cast<ConstantInt>(Start->getArgOperand(0))->getZExtValue();
@@ -606,7 +660,7 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
       auto TagEnd = [&](Instruction *Node) { untagAlloca(AI, Node, Size); };
       if (!DT || !PDT ||
           !memtag::forAllReachableExits(*DT, *PDT, Start, Info.LifetimeEnd,
-                                        SInfo.RetVec, TagEnd)) {
+                                        RetVec, TagEnd)) {
         for (auto *End : Info.LifetimeEnd)
           End->eraseFromParent();
       }
@@ -614,7 +668,7 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
       uint64_t Size = Info.AI->getAllocationSizeInBits(*DL).getValue() / 8;
       Value *Ptr = IRB.CreatePointerCast(TagPCall, IRB.getInt8PtrTy());
       tagAlloca(AI, &*IRB.GetInsertPoint(), Ptr, Size);
-      for (auto &RI : SInfo.RetVec) {
+      for (auto &RI : RetVec) {
         untagAlloca(AI, RI, Size);
       }
       // We may have inserted tag/untag outside of any lifetime interval.
@@ -632,7 +686,7 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
 
   // If we have instrumented at least one alloca, all unrecognized lifetime
   // instrinsics have to go.
-  for (auto &I : SInfo.UnrecognizedLifetimes)
+  for (auto &I : UnrecognizedLifetimes)
     I->eraseFromParent();
 
   return true;

diff  --git a/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp b/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
index b62c391de3386..c149723ace114 100644
--- a/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
+++ b/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
@@ -65,10 +65,8 @@ void StackInfoBuilder::visit(Instruction &Inst) {
     }
   }
   if (AllocaInst *AI = dyn_cast<AllocaInst>(&Inst)) {
-    if (IsInterestingAlloca(*AI)) {
-      Info.AllocasToInstrument[AI].AI = AI;
-      Info.AllocasToInstrument[AI].OldAI = AI;
-    }
+    if (IsInterestingAlloca(*AI))
+      Info.AllocasToInstrument.insert({AI, {}});
     return;
   }
   auto *II = dyn_cast<IntrinsicInst>(&Inst);


        


More information about the llvm-commits mailing list