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 const auto &Result = Legality.canVectorize({St0, St1}); 56 EXPECT_TRUE(isa<sandboxir::Widen>(Result)); 57 58 { 59 // Check NotInstructions 60 auto &Result = Legality.canVectorize({F, St0}); 61 EXPECT_TRUE(isa<sandboxir::Pack>(Result)); 62 EXPECT_EQ(cast<sandboxir::Pack>(Result).getReason(), 63 sandboxir::ResultReason::NotInstructions); 64 } 65 } 66 67 #ifndef NDEBUG 68 TEST_F(LegalityTest, LegalityResultDump) { 69 auto Matches = [](const sandboxir::LegalityResult &Result, 70 const std::string &ExpectedStr) -> bool { 71 std::string Buff; 72 raw_string_ostream OS(Buff); 73 Result.print(OS); 74 return Buff == ExpectedStr; 75 }; 76 sandboxir::LegalityAnalysis Legality; 77 EXPECT_TRUE( 78 Matches(Legality.createLegalityResult<sandboxir::Widen>(), "Widen")); 79 EXPECT_TRUE(Matches(Legality.createLegalityResult<sandboxir::Pack>( 80 sandboxir::ResultReason::NotInstructions), 81 "Pack Reason: NotInstructions")); 82 EXPECT_TRUE(Matches(Legality.createLegalityResult<sandboxir::Pack>( 83 sandboxir::ResultReason::DiffOpcodes), 84 "Pack Reason: DiffOpcodes")); 85 EXPECT_TRUE(Matches(Legality.createLegalityResult<sandboxir::Pack>( 86 sandboxir::ResultReason::DiffTypes), 87 "Pack Reason: DiffTypes")); 88 } 89 #endif // NDEBUG 90