Skip to content

Commit

Permalink
[Clang][BasicAA][AIE2] Enable full PHI AA for AIE
Browse files Browse the repository at this point in the history
Also increase the max limit of the search depth in DecomposeGEPExpression.
With this change we can allow LICM to hoist more instructions.
  • Loading branch information
andcarminati committed Jul 9, 2024
1 parent 623145b commit ac3124f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
6 changes: 6 additions & 0 deletions clang/lib/Driver/ToolChains/AIE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ void AIEToolChain::addClangTargetOptions(
// Make sure to perform most optimizations before mandatory inlinings,
// otherwise noalias attributes can get lost and hurt AA results.
CC1Args.append({"-mllvm", "-mandatory-inlining-before-opt=false"});

// Perform complete AA analysis on phi nodes.
CC1Args.append({"-mllvm", "-basic-aa-full-phi-analysis=true"});

// Extend the max limit of the search depth in BasicAA
CC1Args.append({"-mllvm", "-basic-aa-max-lookup-search-depth=10"});
}

// Avoid using newer dwarf versions, as the simulator doesn't understand newer
Expand Down
16 changes: 11 additions & 5 deletions llvm/lib/Analysis/BasicAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,26 @@ using namespace llvm;
static cl::opt<bool> EnableRecPhiAnalysis("basic-aa-recphi", cl::Hidden,
cl::init(true));

/// Enable full analysis of PHI nodes.
static cl::opt<bool> EnableFullPHIAnalysis("basic-aa-full-phi-analysis",
cl::Hidden, cl::init(false));

static cl::opt<bool> EnableSeparateStorageAnalysis("basic-aa-separate-storage",
cl::Hidden, cl::init(true));

// The max limit of the search depth in DecomposeGEPExpression() and
// getUnderlyingObject().
static cl::opt<unsigned>
MaxLookupSearchDepth("basic-aa-max-lookup-search-depth", cl::Hidden,
cl::init(6));

/// SearchLimitReached / SearchTimes shows how often the limit of
/// to decompose GEPs is reached. It will affect the precision
/// of basic alias analysis.
STATISTIC(SearchLimitReached, "Number of times the limit to "
"decompose GEPs is reached");
STATISTIC(SearchTimes, "Number of times a GEP is decomposed");

// The max limit of the search depth in DecomposeGEPExpression() and
// getUnderlyingObject().
static const unsigned MaxLookupSearchDepth = 6;

bool BasicAAResult::invalidate(Function &Fn, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &Inv) {
// We don't care if this analysis itself is preserved, it has no state. But
Expand Down Expand Up @@ -1389,7 +1395,7 @@ AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize,
if (PV1 == PN)
continue;

if (isa<PHINode>(PV1)) {
if (!EnableFullPHIAnalysis && isa<PHINode>(PV1)) {
if (OnePhi && OnePhi != PV1) {
// To control potential compile time explosion, we choose to be
// conserviate when we have more than one Phi input. It is important
Expand Down
6 changes: 6 additions & 0 deletions llvm/test/Analysis/BasicAA/phi-aa.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -basic-aa-full-phi-analysis \
; RUN: -disable-output 2>&1 | FileCheck %s --check-prefix=FULL-PHI

target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

Expand Down Expand Up @@ -85,6 +88,9 @@ declare void @inc(ptr)
; CHECK: MayAlias: i32* %val1, i32* @Y
; CHECK: MayAlias: i32* %val2, i32* @Y
; CHECK: MayAlias: i32* %val3, i32* @Y
; FULL-PHI: NoAlias: i32* %val1, i32* @Y
; FULL-PHI: NoAlias: i32* %val2, i32* @Y
; FULL-PHI: NoAlias: i32* %val3, i32* @Y
define void @loop_phi_chain(i32 %a, i32 %b, i32 %c) {
entry:
br label %loop1
Expand Down
62 changes: 62 additions & 0 deletions llvm/test/Analysis/BasicAA/phi-full-aa-diamond.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -basic-aa-full-phi-analysis \
; RUN: -disable-output 2>&1 | FileCheck %s --check-prefix=FULL-PHI


; The goal of this test is to test the differences in the behavior of
; the BasicAA with and without full phi analysis in a dual diamond scenario.
; The basic shape of the test is the following (simplified without phis):
; if (...) {
; if (...) {
; %P = getelementptr @X ...
; } else {
; %P = getelementptr @X ...
; }
; } else {
; %P = getelementptr @X ...
; }
;
; // Use of %P and @Y
;

@X = common global i32 0
@Y = common global i32 0

; CHECK: MayAlias: i32* %P, i32* @Y
; FULL-PHI: NoAlias: i32* %P, i32* @Y
define void @foo(i32 %cond) nounwind {
entry:
%"alloca point" = bitcast i32 0 to i32
%tmp = icmp ne i32 %cond, 0
br i1 %tmp, label %bbtrue, label %bbfalse

bbtrue:
%tmp1 = icmp ne i32 %cond, 2
br i1 %tmp, label %bbtruetrue, label %bbtruefalse

bbtruetrue:
%p1 = getelementptr i32, ptr @X, i64 3
br label %bbtrueend

bbtruefalse:
%p2 = getelementptr i32, ptr @X, i64 4
br label %bbtrueend

bbtrueend:
%p3 = phi ptr [ %p1, %bbtruetrue ], [ %p2, %bbtruefalse ]
br label %bblast

bbfalse:
%p4 = getelementptr i32, ptr @X, i64 2
br label %bblast

bblast:
%P = phi ptr [ %p3, %bbtrueend ], [ %p4, %bbfalse ]
%tmp2 = load i32, ptr @Y, align 4
store i32 123, ptr %P, align 4
%tmp3 = load i32, ptr @Y, align 4
br label %return

return:
ret void
}

0 comments on commit ac3124f

Please sign in to comment.