Skip to content

Commit ca998b0

Browse files
author
vporpo
authored
[SandboxVec][Legality] Check wrap flags (#113975)
1 parent 8b55162 commit ca998b0

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum class ResultReason {
3232
DiffOpcodes,
3333
DiffTypes,
3434
DiffMathFlags,
35+
DiffWrapFlags,
3536
};
3637

3738
#ifndef NDEBUG
@@ -56,6 +57,8 @@ struct ToStr {
5657
return "DiffTypes";
5758
case ResultReason::DiffMathFlags:
5859
return "DiffMathFlags";
60+
case ResultReason::DiffWrapFlags:
61+
return "DiffWrapFlags";
5962
}
6063
llvm_unreachable("Unknown ResultReason enum");
6164
}

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ LegalityAnalysis::notVectorizableBasedOnOpcodesAndTypes(
5555
return ResultReason::DiffMathFlags;
5656
}
5757

58+
// TODO: Allow vectorization by using common flags.
59+
// For now Pack if they don't have the same wrap flags.
60+
bool CanHaveWrapFlags =
61+
isa<OverflowingBinaryOperator>(I0) || isa<TruncInst>(I0);
62+
if (CanHaveWrapFlags) {
63+
bool NUW0 = I0->hasNoUnsignedWrap();
64+
bool NSW0 = I0->hasNoSignedWrap();
65+
if (any_of(drop_begin(Bndl), [NUW0, NSW0](auto *V) {
66+
return cast<Instruction>(V)->hasNoUnsignedWrap() != NUW0 ||
67+
cast<Instruction>(V)->hasNoSignedWrap() != NSW0;
68+
})) {
69+
return ResultReason::DiffWrapFlags;
70+
}
71+
}
72+
5873
// TODO: Missing checks
5974

6075
return std::nullopt;

llvm/unittests/Transforms/Vectorize/SandboxVectorizer/LegalityTest.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct LegalityTest : public testing::Test {
2929

3030
TEST_F(LegalityTest, Legality) {
3131
parseIR(C, R"IR(
32-
define void @foo(ptr %ptr, <2 x float> %vec2, <3 x float> %vec3, i8 %arg, float %farg0, float %farg1) {
32+
define void @foo(ptr %ptr, <2 x float> %vec2, <3 x float> %vec3, i8 %arg, float %farg0, float %farg1, i64 %v0, i64 %v1) {
3333
%gep0 = getelementptr float, ptr %ptr, i32 0
3434
%gep1 = getelementptr float, ptr %ptr, i32 1
3535
%gep3 = getelementptr float, ptr %ptr, i32 3
@@ -42,6 +42,8 @@ define void @foo(ptr %ptr, <2 x float> %vec2, <3 x float> %vec3, i8 %arg, float
4242
store i8 %arg, ptr %gep1
4343
%fadd0 = fadd float %farg0, %farg0
4444
%fadd1 = fadd fast float %farg1, %farg1
45+
%trunc0 = trunc nuw nsw i64 %v0 to i8
46+
%trunc1 = trunc nsw i64 %v1 to i8
4547
ret void
4648
}
4749
)IR");
@@ -62,6 +64,8 @@ define void @foo(ptr %ptr, <2 x float> %vec2, <3 x float> %vec3, i8 %arg, float
6264
auto *StI8 = cast<sandboxir::StoreInst>(&*It++);
6365
auto *FAdd0 = cast<sandboxir::BinaryOperator>(&*It++);
6466
auto *FAdd1 = cast<sandboxir::BinaryOperator>(&*It++);
67+
auto *Trunc0 = cast<sandboxir::TruncInst>(&*It++);
68+
auto *Trunc1 = cast<sandboxir::TruncInst>(&*It++);
6569

6670
sandboxir::LegalityAnalysis Legality;
6771
const auto &Result = Legality.canVectorize({St0, St1});
@@ -98,6 +102,13 @@ define void @foo(ptr %ptr, <2 x float> %vec2, <3 x float> %vec3, i8 %arg, float
98102
EXPECT_EQ(cast<sandboxir::Pack>(Result).getReason(),
99103
sandboxir::ResultReason::DiffMathFlags);
100104
}
105+
{
106+
// Check DiffWrapFlags
107+
const auto &Result = Legality.canVectorize({Trunc0, Trunc1});
108+
EXPECT_TRUE(isa<sandboxir::Pack>(Result));
109+
EXPECT_EQ(cast<sandboxir::Pack>(Result).getReason(),
110+
sandboxir::ResultReason::DiffWrapFlags);
111+
}
101112
}
102113

103114
#ifndef NDEBUG
@@ -124,5 +135,8 @@ TEST_F(LegalityTest, LegalityResultDump) {
124135
EXPECT_TRUE(Matches(Legality.createLegalityResult<sandboxir::Pack>(
125136
sandboxir::ResultReason::DiffMathFlags),
126137
"Pack Reason: DiffMathFlags"));
138+
EXPECT_TRUE(Matches(Legality.createLegalityResult<sandboxir::Pack>(
139+
sandboxir::ResultReason::DiffWrapFlags),
140+
"Pack Reason: DiffWrapFlags"));
127141
}
128142
#endif // NDEBUG

0 commit comments

Comments
 (0)