1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
//===- LoopRotationUtilsTest.cpp - Unit tests for LoopRotation utility ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/LoopRotationUtils.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
SMDiagnostic Err;
std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
if (!Mod)
Err.print("LoopRotationUtilsTest", errs());
return Mod;
}
/// This test contains multi-deopt-exits pattern that might allow loop rotation
/// to trigger multiple times if multiple rotations are enabled.
/// At least one rotation should be performed, no matter what loop rotation settings are.
TEST(LoopRotate, MultiDeoptExit) {
LLVMContext C;
std::unique_ptr<Module> M = parseIR(
C,
R"(
declare i32 @llvm.experimental.deoptimize.i32(...)
define i32 @test(i32 * nonnull %a, i64 %x) {
entry:
br label %for.cond1
for.cond1:
%idx = phi i64 [ 0, %entry ], [ %idx.next, %for.tail ]
%sum = phi i32 [ 0, %entry ], [ %sum.next, %for.tail ]
%a.idx = getelementptr inbounds i32, i32 *%a, i64 %idx
%val.a.idx = load i32, i32* %a.idx, align 4
%zero.check = icmp eq i32 %val.a.idx, 0
br i1 %zero.check, label %deopt.exit, label %for.cond2
for.cond2:
%for.check = icmp ult i64 %idx, %x
br i1 %for.check, label %for.body, label %return
for.body:
br label %for.tail
for.tail:
%sum.next = add i32 %sum, %val.a.idx
%idx.next = add nuw nsw i64 %idx, 1
br label %for.cond1
return:
ret i32 %sum
deopt.exit:
%deopt.val = call i32(...) @llvm.experimental.deoptimize.i32() [ "deopt"(i32 %val.a.idx) ]
ret i32 %deopt.val
})"
);
auto *F = M->getFunction("test");
DominatorTree DT(*F);
LoopInfo LI(DT);
AssumptionCache AC(*F);
TargetTransformInfo TTI(M->getDataLayout());
TargetLibraryInfoImpl TLII;
TargetLibraryInfo TLI(TLII);
ScalarEvolution SE(*F, TLI, AC, DT, LI);
SimplifyQuery SQ(M->getDataLayout());
Loop *L = *LI.begin();
bool ret = LoopRotation(L, &LI, &TTI,
&AC, &DT,
&SE, nullptr,
SQ, true, -1, false);
EXPECT_TRUE(ret);
}
/// Checking a special case of multi-deopt exit loop that can not perform
/// required amount of rotations due to the desired header containing
/// non-duplicatable code.
/// Similar to MultiDeoptExit test this one should do at least one rotation and
/// pass no matter what loop rotation settings are.
TEST(LoopRotate, MultiDeoptExit_Nondup) {
LLVMContext C;
std::unique_ptr<Module> M = parseIR(
C,
R"(
; Rotation should be done once, attempted twice.
; Second time fails due to non-duplicatable header.
declare i32 @llvm.experimental.deoptimize.i32(...)
declare void @nondup()
define i32 @test_nondup(i32 * nonnull %a, i64 %x) {
entry:
br label %for.cond1
for.cond1:
%idx = phi i64 [ 0, %entry ], [ %idx.next, %for.tail ]
%sum = phi i32 [ 0, %entry ], [ %sum.next, %for.tail ]
%a.idx = getelementptr inbounds i32, i32 *%a, i64 %idx
%val.a.idx = load i32, i32* %a.idx, align 4
%zero.check = icmp eq i32 %val.a.idx, 0
br i1 %zero.check, label %deopt.exit, label %for.cond2
for.cond2:
call void @nondup() noduplicate
%for.check = icmp ult i64 %idx, %x
br i1 %for.check, label %for.body, label %return
for.body:
br label %for.tail
for.tail:
%sum.next = add i32 %sum, %val.a.idx
%idx.next = add nuw nsw i64 %idx, 1
br label %for.cond1
return:
ret i32 %sum
deopt.exit:
%deopt.val = call i32(...) @llvm.experimental.deoptimize.i32() [ "deopt"(i32 %val.a.idx) ]
ret i32 %deopt.val
})"
);
auto *F = M->getFunction("test_nondup");
DominatorTree DT(*F);
LoopInfo LI(DT);
AssumptionCache AC(*F);
TargetTransformInfo TTI(M->getDataLayout());
TargetLibraryInfoImpl TLII;
TargetLibraryInfo TLI(TLII);
ScalarEvolution SE(*F, TLI, AC, DT, LI);
SimplifyQuery SQ(M->getDataLayout());
Loop *L = *LI.begin();
bool ret = LoopRotation(L, &LI, &TTI,
&AC, &DT,
&SE, nullptr,
SQ, true, -1, false);
/// LoopRotation should properly report "true" as we still perform the first rotation
/// so we do change the IR.
EXPECT_TRUE(ret);
}
|