[llvm] [Attributor] New attribute to identify what byte ranges are alive for an allocation (PR #66148)

Johannes Doerfert via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 27 13:55:20 PDT 2023


================
@@ -12658,6 +12665,232 @@ struct AAAddressSpaceCallSiteArgument final : AAAddressSpaceImpl {
 };
 } // namespace
 
+/// ----------- Allocation Info ----------
+namespace {
+struct AAAllocationInfoImpl : public AAAllocationInfo {
+  AAAllocationInfoImpl(const IRPosition &IRP, Attributor &A)
+      : AAAllocationInfo(IRP, A) {}
+
+  std::optional<TypeSize> getAllocatedSize() const override {
+    assert(isValidState() && "the AA is invalid");
+    return AssumedAllocatedSize;
+  }
+
+  ChangeStatus updateImpl(Attributor &A) override {
+
+    const IRPosition &IRP = getIRPosition();
+    Instruction *I = IRP.getCtxI();
+
+    /*TODO: update check for malloc like calls.*/
+    if (!isa<AllocaInst>(I))
+      return indicatePessimisticFixpoint();
+
+    bool IsKnownNoCapture;
+    if (!AA::hasAssumedIRAttr<Attribute::NoCapture>(
+            A, this, IRP, DepClassTy::OPTIONAL, IsKnownNoCapture))
+      return indicatePessimisticFixpoint();
+
+    if (IsKnownNoCapture)
+      return indicatePessimisticFixpoint();
+
+    const AAPointerInfo *PI =
+        A.getOrCreateAAFor<AAPointerInfo>(IRP, *this, DepClassTy::REQUIRED);
+
+    if (!PI)
+      return indicatePessimisticFixpoint();
+
+    if (!PI->getState().isValidState())
+      return indicatePessimisticFixpoint();
+
+    const DataLayout &DL = A.getDataLayout();
+    int64_t BinSize = PI->numOffsetBins();
+    switch (BinSize) {
+    case 0: {
+      switch (I->getOpcode()) {
+      /*TODO: add case for malloc like calls*/
+      case Instruction::Alloca: {
+        AllocaInst *AI = cast<AllocaInst>(I);
+        const auto AllocationSize = AI->getAllocationSize(DL);
+
+        if (!AllocationSize || *AllocationSize == 0)
+          return indicatePessimisticFixpoint();
+
+        break;
+      }
+      default:
+        return indicatePessimisticFixpoint();
+      }
+
+      auto NewAllocationSize = std::optional<TypeSize>(TypeSize(0, false));
+
+      if (!changeAllocationSize(NewAllocationSize))
+        return ChangeStatus::UNCHANGED;
+      break;
+    }
+    case 1: {
+      const auto &It = PI->begin();
+      if (It->first.Offset == 0) {
+
+        uint64_t SizeOfBin = It->first.Offset + It->first.Size;
+        /*TODO: add case for malloc like calls*/
+        switch (I->getOpcode()) {
+        case Instruction::Alloca: {
+          AllocaInst *AI = cast<AllocaInst>(I);
+          const auto AllocationSize = AI->getAllocationSize(DL);
+
+          if (!AllocationSize || *AllocationSize == 0)
+            return indicatePessimisticFixpoint();
+
+          if (SizeOfBin == *AllocationSize)
+            return indicatePessimisticFixpoint();
+
+          break;
+        }
+        default:
+          return indicatePessimisticFixpoint();
+        }
+
+        auto NewAllocationSize =
+            std::optional<TypeSize>(TypeSize(SizeOfBin * 8, false));
----------------
jdoerfert wrote:

The 0 bin code above and the 1 bin code below are basically the same, except that you initialize offset and size in the former with 0 and 0, and in the latter with It->first.Offset and It->first.Offset + It->first.Size. We don't need the duplication.

https://github.com/llvm/llvm-project/pull/66148


More information about the llvm-commits mailing list