Simon Moll | 733b319 | 2020-03-17 14:52:06 +0100 | [diff] [blame] | 1 | //===- VPIntrinsicTest.cpp - VPIntrinsic unit tests ---------===// |
| 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/ADT/SmallVector.h" |
| 10 | #include "llvm/AsmParser/Parser.h" |
| 11 | #include "llvm/IR/Constants.h" |
| 12 | #include "llvm/IR/IRBuilder.h" |
| 13 | #include "llvm/IR/IntrinsicInst.h" |
| 14 | #include "llvm/IR/LLVMContext.h" |
| 15 | #include "llvm/IR/Module.h" |
| 16 | #include "llvm/IR/Verifier.h" |
| 17 | #include "llvm/Support/SourceMgr.h" |
| 18 | #include "gtest/gtest.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | class VPIntrinsicTest : public testing::Test { |
| 25 | protected: |
| 26 | LLVMContext Context; |
| 27 | |
| 28 | VPIntrinsicTest() : Context() {} |
| 29 | |
| 30 | LLVMContext C; |
| 31 | SMDiagnostic Err; |
| 32 | |
| 33 | std::unique_ptr<Module> CreateVPDeclarationModule() { |
| 34 | return parseAssemblyString( |
| 35 | " declare <8 x i32> @llvm.vp.add.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) " |
| 36 | " declare <8 x i32> @llvm.vp.sub.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) " |
| 37 | " declare <8 x i32> @llvm.vp.mul.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) " |
| 38 | " declare <8 x i32> @llvm.vp.sdiv.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) " |
| 39 | " declare <8 x i32> @llvm.vp.srem.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) " |
| 40 | " declare <8 x i32> @llvm.vp.udiv.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) " |
| 41 | " declare <8 x i32> @llvm.vp.urem.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) " |
| 42 | " declare <8 x i32> @llvm.vp.and.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) " |
| 43 | " declare <8 x i32> @llvm.vp.xor.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) " |
| 44 | " declare <8 x i32> @llvm.vp.or.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) " |
| 45 | " declare <8 x i32> @llvm.vp.ashr.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) " |
| 46 | " declare <8 x i32> @llvm.vp.lshr.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) " |
| 47 | " declare <8 x i32> @llvm.vp.shl.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) ", |
| 48 | Err, C); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | /// Check that VPIntrinsic:canIgnoreVectorLengthParam() returns true |
| 53 | /// if the vector length parameter does not mask off any lanes. |
| 54 | TEST_F(VPIntrinsicTest, CanIgnoreVectorLength) { |
| 55 | LLVMContext C; |
| 56 | SMDiagnostic Err; |
| 57 | |
| 58 | std::unique_ptr<Module> M = |
| 59 | parseAssemblyString( |
| 60 | "declare <256 x i64> @llvm.vp.mul.v256i64(<256 x i64>, <256 x i64>, <256 x i1>, i32)" |
| 61 | "declare <vscale x 2 x i64> @llvm.vp.mul.nxv2i64(<vscale x 2 x i64>, <vscale x 2 x i64>, <vscale x 2 x i1>, i32)" |
| 62 | "declare i32 @llvm.vscale.i32()" |
| 63 | "define void @test_static_vlen( " |
| 64 | " <256 x i64> %i0, <vscale x 2 x i64> %si0," |
| 65 | " <256 x i64> %i1, <vscale x 2 x i64> %si1," |
| 66 | " <256 x i1> %m, <vscale x 2 x i1> %sm, i32 %vl) { " |
| 67 | " %r0 = call <256 x i64> @llvm.vp.mul.v256i64(<256 x i64> %i0, <256 x i64> %i1, <256 x i1> %m, i32 %vl)" |
| 68 | " %r1 = call <256 x i64> @llvm.vp.mul.v256i64(<256 x i64> %i0, <256 x i64> %i1, <256 x i1> %m, i32 256)" |
| 69 | " %r2 = call <256 x i64> @llvm.vp.mul.v256i64(<256 x i64> %i0, <256 x i64> %i1, <256 x i1> %m, i32 0)" |
| 70 | " %r3 = call <256 x i64> @llvm.vp.mul.v256i64(<256 x i64> %i0, <256 x i64> %i1, <256 x i1> %m, i32 7)" |
| 71 | " %r4 = call <256 x i64> @llvm.vp.mul.v256i64(<256 x i64> %i0, <256 x i64> %i1, <256 x i1> %m, i32 123)" |
| 72 | " %vs = call i32 @llvm.vscale.i32()" |
| 73 | " %vs.i64 = mul i32 %vs, 2" |
| 74 | " %r5 = call <vscale x 2 x i64> @llvm.vp.mul.nxv2i64(<vscale x 2 x i64> %si0, <vscale x 2 x i64> %si1, <vscale x 2 x i1> %sm, i32 %vs.i64)" |
| 75 | " %r6 = call <vscale x 2 x i64> @llvm.vp.mul.nxv2i64(<vscale x 2 x i64> %si0, <vscale x 2 x i64> %si1, <vscale x 2 x i1> %sm, i32 99999)" |
| 76 | " ret void " |
| 77 | "}", |
| 78 | Err, C); |
| 79 | |
| 80 | auto *F = M->getFunction("test_static_vlen"); |
| 81 | assert(F); |
| 82 | |
| 83 | const int NumExpected = 7; |
| 84 | const bool Expected[] = {false, true, false, false, false, true, false}; |
| 85 | int i = 0; |
| 86 | for (auto &I : F->getEntryBlock()) { |
| 87 | VPIntrinsic *VPI = dyn_cast<VPIntrinsic>(&I); |
| 88 | if (!VPI) |
| 89 | continue; |
| 90 | |
| 91 | ASSERT_LT(i, NumExpected); |
| 92 | ASSERT_EQ(Expected[i], VPI->canIgnoreVectorLengthParam()); |
| 93 | ++i; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /// Check that the argument returned by |
| 98 | /// VPIntrinsic::Get<X>ParamPos(Intrinsic::ID) has the expected type. |
| 99 | TEST_F(VPIntrinsicTest, GetParamPos) { |
| 100 | std::unique_ptr<Module> M = CreateVPDeclarationModule(); |
| 101 | assert(M); |
| 102 | |
| 103 | for (Function &F : *M) { |
| 104 | ASSERT_TRUE(F.isIntrinsic()); |
| 105 | Optional<int> MaskParamPos = |
| 106 | VPIntrinsic::GetMaskParamPos(F.getIntrinsicID()); |
| 107 | if (MaskParamPos.hasValue()) { |
| 108 | Type *MaskParamType = F.getArg(MaskParamPos.getValue())->getType(); |
| 109 | ASSERT_TRUE(MaskParamType->isVectorTy()); |
Christopher Tetreault | c858deb | 2020-04-17 13:29:38 -0700 | [diff] [blame] | 110 | ASSERT_TRUE(cast<VectorType>(MaskParamType)->getElementType()->isIntegerTy(1)); |
Simon Moll | 733b319 | 2020-03-17 14:52:06 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | Optional<int> VecLenParamPos = |
| 114 | VPIntrinsic::GetVectorLengthParamPos(F.getIntrinsicID()); |
| 115 | if (VecLenParamPos.hasValue()) { |
| 116 | Type *VecLenParamType = F.getArg(VecLenParamPos.getValue())->getType(); |
| 117 | ASSERT_TRUE(VecLenParamType->isIntegerTy(32)); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /// Check that going from Opcode to VP intrinsic and back results in the same |
| 123 | /// Opcode. |
| 124 | TEST_F(VPIntrinsicTest, OpcodeRoundTrip) { |
| 125 | std::vector<unsigned> Opcodes; |
| 126 | Opcodes.reserve(100); |
| 127 | |
| 128 | { |
| 129 | #define HANDLE_INST(OCNum, OCName, Class) Opcodes.push_back(OCNum); |
| 130 | #include "llvm/IR/Instruction.def" |
| 131 | } |
| 132 | |
| 133 | unsigned FullTripCounts = 0; |
| 134 | for (unsigned OC : Opcodes) { |
| 135 | Intrinsic::ID VPID = VPIntrinsic::GetForOpcode(OC); |
| 136 | // no equivalent VP intrinsic available |
| 137 | if (VPID == Intrinsic::not_intrinsic) |
| 138 | continue; |
| 139 | |
| 140 | unsigned RoundTripOC = VPIntrinsic::GetFunctionalOpcodeForVP(VPID); |
| 141 | // no equivalent Opcode available |
| 142 | if (RoundTripOC == Instruction::Call) |
| 143 | continue; |
| 144 | |
| 145 | ASSERT_EQ(RoundTripOC, OC); |
| 146 | ++FullTripCounts; |
| 147 | } |
| 148 | ASSERT_NE(FullTripCounts, 0u); |
| 149 | } |
| 150 | |
| 151 | } // end anonymous namespace |