xref: /llvm-project/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/LegalityTest.cpp (revision eb9f4756bc3daaa4b19f4f46521dc05180814de4)
1 //===- LegalityTest.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/SandboxIR/Function.h"
12 #include "llvm/SandboxIR/Instruction.h"
13 #include "llvm/Support/SourceMgr.h"
14 #include "gtest/gtest.h"
15 
16 using namespace llvm;
17 
18 struct LegalityTest : public testing::Test {
19   LLVMContext C;
20   std::unique_ptr<Module> M;
21 
22   void parseIR(LLVMContext &C, const char *IR) {
23     SMDiagnostic Err;
24     M = parseAssemblyString(IR, Err, C);
25     if (!M)
26       Err.print("LegalityTest", errs());
27   }
28 };
29 
30 TEST_F(LegalityTest, Legality) {
31   parseIR(C, R"IR(
32 define void @foo(ptr %ptr) {
33   %gep0 = getelementptr float, ptr %ptr, i32 0
34   %gep1 = getelementptr float, ptr %ptr, i32 1
35   %ld0 = load float, ptr %gep0
36   %ld1 = load float, ptr %gep0
37   store float %ld0, ptr %gep0
38   store float %ld1, ptr %gep1
39   ret void
40 }
41 )IR");
42   llvm::Function *LLVMF = &*M->getFunction("foo");
43   sandboxir::Context Ctx(C);
44   auto *F = Ctx.createFunction(LLVMF);
45   auto *BB = &*F->begin();
46   auto It = BB->begin();
47   [[maybe_unused]] auto *Gep0 = cast<sandboxir::GetElementPtrInst>(&*It++);
48   [[maybe_unused]] auto *Gep1 = cast<sandboxir::GetElementPtrInst>(&*It++);
49   [[maybe_unused]] auto *Ld0 = cast<sandboxir::LoadInst>(&*It++);
50   [[maybe_unused]] auto *Ld1 = cast<sandboxir::LoadInst>(&*It++);
51   auto *St0 = cast<sandboxir::StoreInst>(&*It++);
52   auto *St1 = cast<sandboxir::StoreInst>(&*It++);
53 
54   sandboxir::LegalityAnalysis Legality;
55   auto Result = Legality.canVectorize({St0, St1});
56   EXPECT_TRUE(isa<sandboxir::Widen>(Result));
57 }
58 
59 #ifndef NDEBUG
60 TEST_F(LegalityTest, LegalityResultDump) {
61   auto Matches = [](const sandboxir::LegalityResult &Result,
62                     const std::string &ExpectedStr) -> bool {
63     std::string Buff;
64     raw_string_ostream OS(Buff);
65     Result.print(OS);
66     return Buff == ExpectedStr;
67   };
68   sandboxir::LegalityAnalysis Legality;
69   EXPECT_TRUE(
70       Matches(Legality.createLegalityResult<sandboxir::Widen>(), "Widen"));
71   EXPECT_TRUE(Matches(Legality.createLegalityResult<sandboxir::Pack>(
72                           sandboxir::ResultReason::DiffOpcodes),
73                       "Pack Reason: DiffOpcodes"));
74   EXPECT_TRUE(Matches(Legality.createLegalityResult<sandboxir::Pack>(
75                           sandboxir::ResultReason::DiffTypes),
76                       "Pack Reason: DiffTypes"));
77 }
78 #endif // NDEBUG
79