[PATCH] D119604: AttributorAttributes: avoid a crashing on bad alignments
Augie Fackler via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 11 16:15:53 PST 2022
durin42 created this revision.
Herald added subscribers: ormris, okura, jdoerfert, kuter, uenoku, hiraditya.
Herald added a reviewer: uenoku.
durin42 requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added a reviewer: sstefan1.
Herald added a reviewer: baziotis.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Prior to this change, LLVM would attempt to optimize an
aligned_alloc(33, ...) call to the stack. This flunked an assertion when
trying to emit the alloca, which crashed LLVM. Avoid that with extra
checks.
Depends on D119583 <https://reviews.llvm.org/D119583>
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D119604
Files:
llvm/lib/Transforms/IPO/AttributorAttributes.cpp
llvm/test/Transforms/Attributor/heap_to_stack.ll
Index: llvm/test/Transforms/Attributor/heap_to_stack.ll
===================================================================
--- llvm/test/Transforms/Attributor/heap_to_stack.ll
+++ llvm/test/Transforms/Attributor/heap_to_stack.ll
@@ -218,6 +218,20 @@
ret void
}
+; leave alone a constant-but-invalid alignment
+define void @test3d(i8* %p) {
+; CHECK-LABEL: define {{[^@]+}}@test3d
+; CHECK-SAME; (i8* nocapture [[P:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = tail call noalias i8* @aligned_alloc(i64 allocalign noundef 33, i64 noundef 128)
+; CHECK: tail call void @free(i8* noalias nocapture [[TMP1]])
+; CHECK-NEXT: ret void
+;
+ %1 = tail call noalias i8* @aligned_alloc(i64 allocalign 33, i64 128)
+ tail call void @nofree_arg_only(i8* %1, i8* %p)
+ tail call void @free(i8* %1)
+ ret void
+}
+
declare noalias i8* @calloc(i64, i64)
define void @test0() {
Index: llvm/lib/Transforms/IPO/AttributorAttributes.cpp
===================================================================
--- llvm/lib/Transforms/IPO/AttributorAttributes.cpp
+++ llvm/lib/Transforms/IPO/AttributorAttributes.cpp
@@ -36,12 +36,14 @@
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/Value.h"
#include "llvm/IR/NoFolder.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO/ArgumentPromotion.h"
#include "llvm/Transforms/Utils/Local.h"
@@ -6331,7 +6333,8 @@
continue;
if (Value *Align = getAllocAlignment(AI.CB, TLI)) {
- if (!getAPInt(A, *this, *Align)) {
+ Optional<APInt> APAlign = getAPInt(A, *this, *Align);
+ if (!APAlign) {
// Can't generate an alloca which respects the required alignment
// on the allocation.
LLVM_DEBUG(dbgs() << "[H2S] Unknown allocation alignment: " << *AI.CB
@@ -6339,6 +6342,14 @@
AI.Status = AllocationInfo::INVALID;
Changed = ChangeStatus::CHANGED;
continue;
+ } else {
+ uint64_t AlignInt = APAlign.getValue().getZExtValue();
+ if (AlignInt > llvm::Value::MaximumAlignment || !isPowerOf2_64(AlignInt)) {
+ LLVM_DEBUG(dbgs() << "[H2S] Invalid allocation alignment: " << AlignInt << "\n");
+ AI.Status = AllocationInfo::INVALID;
+ Changed = ChangeStatus::CHANGED;
+ continue;
+ }
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119604.408102.patch
Type: text/x-patch
Size: 2592 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220212/62722e88/attachment.bin>
More information about the llvm-commits
mailing list