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/Instruction.h" 12 #include "llvm/Support/SourceMgr.h" 13 #include "gtest/gtest.h" 14 15 using namespace llvm; 16 17 struct LegalityTest : public testing::Test { 18 LLVMContext C; 19 std::unique_ptr<Module> M; 20 21 void parseIR(LLVMContext &C, const char *IR) { 22 SMDiagnostic Err; 23 M = parseAssemblyString(IR, Err, C); 24 if (!M) 25 Err.print("LegalityTest", errs()); 26 } 27 }; 28 29 TEST_F(LegalityTest, Legality) { 30 parseIR(C, R"IR( 31 define void @foo(ptr %ptr) { 32 %gep0 = getelementptr float, ptr %ptr, i32 0 33 %gep1 = getelementptr float, ptr %ptr, i32 1 34 %ld0 = load float, ptr %gep0 35 %ld1 = load float, ptr %gep0 36 store float %ld0, ptr %gep0 37 store float %ld1, ptr %gep1 38 ret void 39 } 40 )IR"); 41 llvm::Function *LLVMF = &*M->getFunction("foo"); 42 sandboxir::Context Ctx(C); 43 auto *F = Ctx.createFunction(LLVMF); 44 auto *BB = &*F->begin(); 45 auto It = BB->begin(); 46 [[maybe_unused]] auto *Gep0 = cast<sandboxir::GetElementPtrInst>(&*It++); 47 [[maybe_unused]] auto *Gep1 = cast<sandboxir::GetElementPtrInst>(&*It++); 48 [[maybe_unused]] auto *Ld0 = cast<sandboxir::LoadInst>(&*It++); 49 [[maybe_unused]] auto *Ld1 = cast<sandboxir::LoadInst>(&*It++); 50 auto *St0 = cast<sandboxir::StoreInst>(&*It++); 51 auto *St1 = cast<sandboxir::StoreInst>(&*It++); 52 53 sandboxir::LegalityAnalysis Legality; 54 auto Result = Legality.canVectorize({St0, St1}); 55 EXPECT_TRUE(isa<sandboxir::Widen>(Result)); 56 } 57