[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:59:46 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());
----------------
vitalybuka wrote:
there is similar block on L720
do we want a helper here?
https://github.com/llvm/llvm-project/pull/236278
More information about the llvm-branch-commits
mailing list