[llvm-branch-commits] [llvm] [AArch64][MTE] Tag dynamic allocas in AArch64StackTagging (PR #236278)

Vitaly Buka via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Nov 5 10:56:13 PST 2026


================
@@ -431,10 +441,135 @@ static Value *getSlotPtr(const AArch64Options &CLOpts, IRBuilder<> &IRB,
   return nullptr;
 }
 
+void AArch64StackTagging::createDynamicAllocasInitStorage() {
+  IRBuilder<> IRB(&*F->getEntryBlock().getFirstInsertionPt());
+  Type *IntptrTy = IRB.getIntPtrTy(*DL);
+  DynamicAllocaLayout =
+      IRB.CreateAlloca(IntptrTy, nullptr, "dynamic.alloca.layout");
+  // 0 means that no alloca has been made.
+  IRB.CreateStore(Constant::getNullValue(IntptrTy), DynamicAllocaLayout);
+  // Its address is the end of the memory untagged on return, which must be
+  // granule aligned.
+  DynamicAllocaLayout->setAlignment(kTagGranuleSize);
+}
+
+// Replace the dynamic alloca with one whose size is a multiple of the tag
+// granule, and tag the memory it returns.
+void AArch64StackTagging::handleDynamicAllocaCall(memtag::AllocaInfo &Info,
+                                                  Value *Base, unsigned Tag) {
+  AllocaInst *AI = Info.AI;
+  IRBuilder<> IRB(AI);
+  Type *IntptrTy = IRB.getIntPtrTy(*DL);
+  const uint64_t Granule = kTagGranuleSize.value();
+  Value *Size = IRB.CreateAllocationSize(IntptrTy, AI);
+  // Rounded up to a multiple of the granule.
+  Value *PaddedSize = IRB.CreateAnd(
+      IRB.CreateAdd(Size, ConstantInt::get(IntptrTy, Granule - 1)),
+      ConstantInt::get(IntptrTy, ~(Granule - 1)));
+
+  AllocaInst *NewAI =
+      IRB.CreateAlloca(IRB.getInt8Ty(), AI->getAddressSpace(), PaddedSize);
+  NewAI->setAlignment(std::max(AI->getAlign(), kTagGranuleSize));
+  NewAI->takeName(AI);
+  NewAI->copyMetadata(*AI);
+  AI->replaceAllUsesWith(NewAI);
+  AI->eraseFromParent();
+  Info.AI = NewAI;
+
+  // Replace alloca with tagp(alloca).
+  IRB.SetInsertPoint(NewAI->getNextNode());
+  Instruction *TagPCall = IRB.CreateIntrinsicWithoutFolding(
+      Intrinsic::aarch64_tagp, {NewAI->getType()},
+      {Constant::getNullValue(NewAI->getType()), Base, IRB.getInt64(Tag)});
+  if (NewAI->hasName())
+    TagPCall->setName(NewAI->getName() + ".tag");
+  // Does not replace metadata, so we don't have to handle DbgVariableRecords.
+  NewAI->replaceUsesWithIf(TagPCall, [&](const Use &U) {
+    return !isa<LifetimeIntrinsic>(U.getUser());
+  });
+  TagPCall->setOperand(0, NewAI);
+
+  // Store the last alloca's address to DynamicAllocaLayout. This points to the
+  // most recently executed dynamic alloca, which is used when untagging. It is
+  // granule aligned.
+  IRB.CreateStore(IRB.CreatePtrToInt(NewAI, IntptrTy), DynamicAllocaLayout);
+
+  // Tag the memory at llvm.lifetime.start if there is one, and untag it at
+  // llvm.lifetime.end. NewAI, and so TagPCall, is granule aligned, and TagSize
----------------
vitalybuka wrote:

`StackInfoBuilder::visit` already populates `Info.LifetimeStart` and `Info.LifetimeEnd`
Can use `Info` directly instead of walking `NewAI->users()` here?

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


More information about the llvm-branch-commits mailing list