| 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/CodeGen/ISDOpcodes.h" |
| 12 |
#include "llvm/IR/Constants.h" |
| 13 |
#include "llvm/IR/IRBuilder.h" |
| 14 |
#include "llvm/IR/IntrinsicInst.h" |
| 15 |
#include "llvm/IR/LLVMContext.h" |
| 16 |
#include "llvm/IR/Module.h" |
| 17 |
#include "llvm/IR/Verifier.h" |
| 18 |
#include "llvm/Support/SourceMgr.h" |
| 19 |
#include "gtest/gtest.h" |
| 20 |
#include <optional> |
| 21 |
#include <sstream> |
| 22 |
|
| 23 |
using namespace llvm; |
| 24 |
|
| 25 |
namespace { |
| 26 |
|
| 27 |
static const char *ReductionIntOpcodes[] = { |
| 28 |
"add", "mul", "and", "or", "xor", "smin", "smax", "umin", "umax"}; |
| 29 |
|
| 30 |
static const char *ReductionFPOpcodes[] = {"fadd", "fmul", "fmin", "fmax"}; |
| 31 |
|
| 32 |
class VPIntrinsicTest : public testing::Test { |
| 33 |
protected: |
| 34 |
LLVMContext Context; |
| 35 |
|
| 36 |
VPIntrinsicTest() : Context() {} |
| 37 |
|
| 38 |
LLVMContext C; |
| 39 |
SMDiagnostic Err; |
| 40 |
|
| 41 |
std::unique_ptr<Module> createVPDeclarationModule() { |
| 42 |
const char *BinaryIntOpcodes[] = {"add", "sub", "mul", "sdiv", "srem", |
| 43 |
"udiv", "urem", "and", "xor", "or", |
| 44 |
"ashr", "lshr", "shl", "smin", "smax", |
| 45 |
"umin", "umax"}; |
| 46 |
std::stringstream Str; |
| 47 |
for (const char *BinaryIntOpcode : BinaryIntOpcodes) |
| 48 |
Str << " declare <8 x i32> @llvm.vp." << BinaryIntOpcode |
| 49 |
<< ".v8i32(<8 x i32>, <8 x i32>, <8 x i1>, i32) "; |
| 50 |
|
| 51 |
const char *BinaryFPOpcodes[] = {"fadd", "fsub", "fmul", "fdiv", |
| 52 |
"frem", "minnum", "maxnum", "copysign"}; |
| 53 |
for (const char *BinaryFPOpcode : BinaryFPOpcodes) |
| 54 |
Str << " declare <8 x float> @llvm.vp." << BinaryFPOpcode |
| 55 |
<< ".v8f32(<8 x float>, <8 x float>, <8 x i1>, i32) "; |
| 56 |
|
| 57 |
Str << " declare <8 x float> @llvm.vp.floor.v8f32(<8 x float>, <8 x i1>, " |
| 58 |
"i32)"; |
| 59 |
Str << " declare <8 x float> @llvm.vp.round.v8f32(<8 x float>, <8 x i1>, " |
| 60 |
"i32)"; |
| 61 |
Str << " declare <8 x float> @llvm.vp.roundeven.v8f32(<8 x float>, <8 x " |
| 62 |
"i1>, " |
| 63 |
"i32)"; |
| 64 |
Str << " declare <8 x float> @llvm.vp.roundtozero.v8f32(<8 x float>, <8 x " |
| 65 |
"i1>, " |
| 66 |
"i32)"; |
| 67 |
Str << " declare <8 x float> @llvm.vp.rint.v8f32(<8 x float>, <8 x i1>, " |
| 68 |
"i32)"; |
| 69 |
Str << " declare <8 x float> @llvm.vp.nearbyint.v8f32(<8 x float>, <8 x " |
| 70 |
"i1>, " |
| 71 |
"i32)"; |
| 72 |
Str << " declare <8 x float> @llvm.vp.ceil.v8f32(<8 x float>, <8 x i1>, " |
| 73 |
"i32)"; |
| 74 |
Str << " declare <8 x float> @llvm.vp.fneg.v8f32(<8 x float>, <8 x i1>, " |
| 75 |
"i32)"; |
| 76 |
Str << " declare <8 x float> @llvm.vp.fabs.v8f32(<8 x float>, <8 x i1>, " |
| 77 |
"i32)"; |
| 78 |
Str << " declare <8 x float> @llvm.vp.sqrt.v8f32(<8 x float>, <8 x i1>, " |
| 79 |
"i32)"; |
| 80 |
Str << " declare <8 x float> @llvm.vp.fma.v8f32(<8 x float>, <8 x float>, " |
| 81 |
"<8 x float>, <8 x i1>, i32) "; |
| 82 |
Str << " declare <8 x float> @llvm.vp.fmuladd.v8f32(<8 x float>, " |
| 83 |
"<8 x float>, <8 x float>, <8 x i1>, i32) "; |
| 84 |
|
| 85 |
Str << " declare void @llvm.vp.store.v8i32.p0v8i32(<8 x i32>, <8 x i32>*, " |
| 86 |
"<8 x i1>, i32) "; |
| 87 |
Str << "declare void " |
| 88 |
"@llvm.experimental.vp.strided.store.v8i32.i32(<8 x i32>, " |
| 89 |
"i32*, i32, <8 x i1>, i32) "; |
| 90 |
Str << "declare void " |
| 91 |
"@llvm.experimental.vp.strided.store.v8i32.p1i32.i32(<8 x i32>, " |
| 92 |
"i32 addrspace(1)*, i32, <8 x i1>, i32) "; |
| 93 |
Str << " declare void @llvm.vp.scatter.v8i32.v8p0i32(<8 x i32>, <8 x " |
| 94 |
"i32*>, <8 x i1>, i32) "; |
| 95 |
Str << " declare <8 x i32> @llvm.vp.load.v8i32.p0v8i32(<8 x i32>*, <8 x " |
| 96 |
"i1>, i32) "; |
| 97 |
Str << "declare <8 x i32> " |
| 98 |
"@llvm.experimental.vp.strided.load.v8i32.i32(i32*, i32, <8 " |
| 99 |
"x i1>, i32) "; |
| 100 |
Str << "declare <8 x i32> " |
| 101 |
"@llvm.experimental.vp.strided.load.v8i32.p1i32.i32(i32 " |
| 102 |
"addrspace(1)*, i32, <8 x i1>, i32) "; |
| 103 |
Str << " declare <8 x i32> @llvm.vp.gather.v8i32.v8p0i32(<8 x i32*>, <8 x " |
| 104 |
"i1>, i32) "; |
| 105 |
|
| 106 |
for (const char *ReductionOpcode : ReductionIntOpcodes) |
| 107 |
Str << " declare i32 @llvm.vp.reduce." << ReductionOpcode |
| 108 |
<< ".v8i32(i32, <8 x i32>, <8 x i1>, i32) "; |
| 109 |
|
| 110 |
for (const char *ReductionOpcode : ReductionFPOpcodes) |
| 111 |
Str << " declare float @llvm.vp.reduce." << ReductionOpcode |
| 112 |
<< ".v8f32(float, <8 x float>, <8 x i1>, i32) "; |
| 113 |
|
| 114 |
Str << " declare <8 x i32> @llvm.vp.merge.v8i32(<8 x i1>, <8 x i32>, <8 x " |
| 115 |
"i32>, i32)"; |
| 116 |
Str << " declare <8 x i32> @llvm.vp.select.v8i32(<8 x i1>, <8 x i32>, <8 x " |
| 117 |
"i32>, i32)"; |
| 118 |
Str << " declare <8 x i32> @llvm.experimental.vp.splice.v8i32(<8 x " |
| 119 |
"i32>, <8 x i32>, i32, <8 x i1>, i32, i32) "; |
| 120 |
|
| 121 |
Str << " declare <8 x i32> @llvm.vp.fptoui.v8i32" |
| 122 |
<< ".v8f32(<8 x float>, <8 x i1>, i32) "; |
| 123 |
Str << " declare <8 x i32> @llvm.vp.fptosi.v8i32" |
| 124 |
<< ".v8f32(<8 x float>, <8 x i1>, i32) "; |
| 125 |
Str << " declare <8 x float> @llvm.vp.uitofp.v8f32" |
| 126 |
<< ".v8i32(<8 x i32>, <8 x i1>, i32) "; |
| 127 |
Str << " declare <8 x float> @llvm.vp.sitofp.v8f32" |
| 128 |
<< ".v8i32(<8 x i32>, <8 x i1>, i32) "; |
| 129 |
Str << " declare <8 x float> @llvm.vp.fptrunc.v8f32" |
| 130 |
<< ".v8f64(<8 x double>, <8 x i1>, i32) "; |
| 131 |
Str << " declare <8 x double> @llvm.vp.fpext.v8f64" |
| 132 |
<< ".v8f32(<8 x float>, <8 x i1>, i32) "; |
| 133 |
Str << " declare <8 x i32> @llvm.vp.trunc.v8i32" |
| 134 |
<< ".v8i64(<8 x i64>, <8 x i1>, i32) "; |
| 135 |
Str << " declare <8 x i64> @llvm.vp.zext.v8i64" |
| 136 |
<< ".v8i32(<8 x i32>, <8 x i1>, i32) "; |
| 137 |
Str << " declare <8 x i64> @llvm.vp.sext.v8i64" |
| 138 |
<< ".v8i32(<8 x i32>, <8 x i1>, i32) "; |
| 139 |
Str << " declare <8 x i32> @llvm.vp.ptrtoint.v8i32" |
| 140 |
<< ".v8p0i32(<8 x i32*>, <8 x i1>, i32) "; |
| 141 |
Str << " declare <8 x i32*> @llvm.vp.inttoptr.v8p0i32" |
| 142 |
<< ".v8i32(<8 x i32>, <8 x i1>, i32) "; |
| 143 |
|
| 144 |
Str << " declare <8 x i1> @llvm.vp.fcmp.v8f32" |
| 145 |
<< "(<8 x float>, <8 x float>, metadata, <8 x i1>, i32) "; |
| 146 |
Str << " declare <8 x i1> @llvm.vp.icmp.v8i16" |
| 147 |
<< "(<8 x i16>, <8 x i16>, metadata, <8 x i1>, i32) "; |
| 148 |
|
| 149 |
Str << " declare <8 x i16> @llvm.vp.abs.v8i16" |
| 150 |
<< "(<8 x i16>, <8 x i1>, i32, i1 immarg) "; |
| 151 |
Str << " declare <8 x i16> @llvm.vp.bitreverse.v8i16" |
| 152 |
<< "(<8 x i16>, <8 x i1>, i32) "; |
| 153 |
Str << " declare <8 x i16> @llvm.vp.bswap.v8i16" |
| 154 |
<< "(<8 x i16>, <8 x i1>, i32) "; |
| 155 |
Str << " declare <8 x i16> @llvm.vp.ctpop.v8i16" |
| 156 |
<< "(<8 x i16>, <8 x i1>, i32) "; |
| 157 |
Str << " declare <8 x i16> @llvm.vp.ctlz.v8i16" |
| 158 |
<< "(<8 x i16>, <8 x i1>, i32, i1 immarg) "; |
| 159 |
Str << " declare <8 x i16> @llvm.vp.cttz.v8i16" |
| 160 |
<< "(<8 x i16>, <8 x i1>, i32, i1 immarg) "; |
| 161 |
Str << " declare <8 x i16> @llvm.vp.fshl.v8i16" |
| 162 |
<< "(<8 x i16>, <8 x i16>, <8 x i16>, <8 x i1>, i32) "; |
| 163 |
Str << " declare <8 x i16> @llvm.vp.fshr.v8i16" |
| 164 |
<< "(<8 x i16>, <8 x i16>, <8 x i16>, <8 x i1>, i32) "; |
| 165 |
|
| 166 |
return parseAssemblyString(Str.str(), Err, C); |
| 167 |
} |
| 168 |
}; |
| 169 |
|
| 170 |
/// Check that the property scopes include/llvm/IR/VPIntrinsics.def are closed. |
| 171 |
TEST_F(VPIntrinsicTest, VPIntrinsicsDefScopes) { |
| 172 |
std::optional<Intrinsic::ID> ScopeVPID; |
| 173 |
#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) \ |
| 174 |
ASSERT_FALSE(ScopeVPID.has_value()); \ |
| 175 |
ScopeVPID = Intrinsic::VPID; |
| 176 |
#define END_REGISTER_VP_INTRINSIC(VPID) \ |
| 177 |
ASSERT_TRUE(ScopeVPID.has_value()); \ |
| 178 |
ASSERT_EQ(*ScopeVPID, Intrinsic::VPID); \ |
| 179 |
ScopeVPID = std::nullopt; |
| 180 |
|
| 181 |
std::optional<ISD::NodeType> ScopeOPC; |
| 182 |
#define BEGIN_REGISTER_VP_SDNODE(SDOPC, ...) \ |
| 183 |
ASSERT_FALSE(ScopeOPC.has_value()); \ |
| 184 |
ScopeOPC = ISD::SDOPC; |
| 185 |
#define END_REGISTER_VP_SDNODE(SDOPC) \ |
| 186 |
ASSERT_TRUE(ScopeOPC.has_value()); \ |
| 187 |
ASSERT_EQ(*ScopeOPC, ISD::SDOPC); \ |
| 188 |
ScopeOPC = std::nullopt; |
| 189 |
#include "llvm/IR/VPIntrinsics.def" |
| 190 |
|
| 191 |
ASSERT_FALSE(ScopeVPID.has_value()); |
| 192 |
ASSERT_FALSE(ScopeOPC.has_value()); |
| 193 |
} |
| 194 |
|
| 195 |
/// Check that every VP intrinsic in the test module is recognized as a VP |
| 196 |
/// intrinsic. |
| 197 |
TEST_F(VPIntrinsicTest, VPModuleComplete) { |
| 198 |
std::unique_ptr<Module> M = createVPDeclarationModule(); |
| 199 |
assert(M); |
| 200 |
|
| 201 |
// Check that all @llvm.vp.* functions in the module are recognized vp |
| 202 |
// intrinsics. |
| 203 |
std::set<Intrinsic::ID> SeenIDs; |
| 204 |
for (const auto &VPDecl : *M) { |
| 205 |
ASSERT_TRUE(VPDecl.isIntrinsic()); |
| 206 |
ASSERT_TRUE(VPIntrinsic::isVPIntrinsic(VPDecl.getIntrinsicID())); |
| 207 |
SeenIDs.insert(VPDecl.getIntrinsicID()); |
| 208 |
} |
| 209 |
|
| 210 |
// Check that every registered VP intrinsic has an instance in the test |
| 211 |
// module. |
| 212 |
#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) \ |
| 213 |
ASSERT_TRUE(SeenIDs.count(Intrinsic::VPID)); |
| 214 |
#include "llvm/IR/VPIntrinsics.def" |
| 215 |
} |
| 216 |
|
| 217 |
/// Check that VPIntrinsic:canIgnoreVectorLengthParam() returns true |
| 218 |
/// if the vector length parameter does not mask off any lanes. |
| 219 |
TEST_F(VPIntrinsicTest, CanIgnoreVectorLength) { |
| 220 |
LLVMContext C; |
| 221 |
SMDiagnostic Err; |
| 222 |
|
| 223 |
std::unique_ptr<Module> M = |
| 224 |
parseAssemblyString( |
| 225 |
"declare <256 x i64> @llvm.vp.mul.v256i64(<256 x i64>, <256 x i64>, <256 x i1>, i32)" |
| 226 |
"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)" |
| 227 |
"declare <vscale x 1 x i64> @llvm.vp.mul.nxv1i64(<vscale x 1 x i64>, <vscale x 1 x i64>, <vscale x 1 x i1>, i32)" |
| 228 |
"declare i32 @llvm.vscale.i32()" |
| 229 |
"define void @test_static_vlen( " |
| 230 |
" <256 x i64> %i0, <vscale x 2 x i64> %si0x2, <vscale x 1 x i64> %si0x1," |
| 231 |
" <256 x i64> %i1, <vscale x 2 x i64> %si1x2, <vscale x 1 x i64> %si1x1," |
| 232 |
" <256 x i1> %m, <vscale x 2 x i1> %smx2, <vscale x 1 x i1> %smx1, i32 %vl) { " |
| 233 |
" %r0 = call <256 x i64> @llvm.vp.mul.v256i64(<256 x i64> %i0, <256 x i64> %i1, <256 x i1> %m, i32 %vl)" |
| 234 |
" %r1 = call <256 x i64> @llvm.vp.mul.v256i64(<256 x i64> %i0, <256 x i64> %i1, <256 x i1> %m, i32 256)" |
| 235 |
" %r2 = call <256 x i64> @llvm.vp.mul.v256i64(<256 x i64> %i0, <256 x i64> %i1, <256 x i1> %m, i32 0)" |
| 236 |
" %r3 = call <256 x i64> @llvm.vp.mul.v256i64(<256 x i64> %i0, <256 x i64> %i1, <256 x i1> %m, i32 7)" |
| 237 |
" %r4 = call <256 x i64> @llvm.vp.mul.v256i64(<256 x i64> %i0, <256 x i64> %i1, <256 x i1> %m, i32 123)" |
| 238 |
" %vs = call i32 @llvm.vscale.i32()" |
| 239 |
" %vs.x2 = mul i32 %vs, 2" |
| 240 |
" %r5 = call <vscale x 2 x i64> @llvm.vp.mul.nxv2i64(<vscale x 2 x i64> %si0x2, <vscale x 2 x i64> %si1x2, <vscale x 2 x i1> %smx2, i32 %vs.x2)" |
| 241 |
" %r6 = call <vscale x 2 x i64> @llvm.vp.mul.nxv2i64(<vscale x 2 x i64> %si0x2, <vscale x 2 x i64> %si1x2, <vscale x 2 x i1> %smx2, i32 %vs)" |
| 242 |
" %r7 = call <vscale x 2 x i64> @llvm.vp.mul.nxv2i64(<vscale x 2 x i64> %si0x2, <vscale x 2 x i64> %si1x2, <vscale x 2 x i1> %smx2, i32 99999)" |
| 243 |
" %r8 = call <vscale x 1 x i64> @llvm.vp.mul.nxv1i64(<vscale x 1 x i64> %si0x1, <vscale x 1 x i64> %si1x1, <vscale x 1 x i1> %smx1, i32 %vs)" |
| 244 |
" %r9 = call <vscale x 1 x i64> @llvm.vp.mul.nxv1i64(<vscale x 1 x i64> %si0x1, <vscale x 1 x i64> %si1x1, <vscale x 1 x i1> %smx1, i32 1)" |
| 245 |
" %r10 = call <vscale x 1 x i64> @llvm.vp.mul.nxv1i64(<vscale x 1 x i64> %si0x1, <vscale x 1 x i64> %si1x1, <vscale x 1 x i1> %smx1, i32 %vs.x2)" |
| 246 |
" %vs.wat = add i32 %vs, 2" |
| 247 |
" %r11 = call <vscale x 2 x i64> @llvm.vp.mul.nxv2i64(<vscale x 2 x i64> %si0x2, <vscale x 2 x i64> %si1x2, <vscale x 2 x i1> %smx2, i32 %vs.wat)" |
| 248 |
" ret void " |
| 249 |
"}", |
| 250 |
Err, C); |
| 251 |
|
| 252 |
auto *F = M->getFunction("test_static_vlen"); |
| 253 |
assert(F); |
| 254 |
|
| 255 |
const bool Expected[] = {false, true, false, false, false, true, |
| 256 |
false, false, true, false, true, false}; |
| 257 |
const auto *ExpectedIt = std::begin(Expected); |
| 258 |
for (auto &I : F->getEntryBlock()) { |
| 259 |
VPIntrinsic *VPI = dyn_cast<VPIntrinsic>(&I); |
| 260 |
if (!VPI) |
| 261 |
continue; |
| 262 |
|
| 263 |
ASSERT_NE(ExpectedIt, std::end(Expected)); |
| 264 |
ASSERT_EQ(*ExpectedIt, VPI->canIgnoreVectorLengthParam()); |
| 265 |
++ExpectedIt; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
/// Check that the argument returned by |
| 270 |
/// VPIntrinsic::get<X>ParamPos(Intrinsic::ID) has the expected type. |
| 271 |
TEST_F(VPIntrinsicTest, GetParamPos) { |
| 272 |
std::unique_ptr<Module> M = createVPDeclarationModule(); |
| 273 |
assert(M); |
| 274 |
|
| 275 |
for (Function &F : *M) { |
| 276 |
ASSERT_TRUE(F.isIntrinsic()); |
| 277 |
std::optional<unsigned> MaskParamPos = |
| 278 |
VPIntrinsic::getMaskParamPos(F.getIntrinsicID()); |
| 279 |
if (MaskParamPos) { |
| 280 |
Type *MaskParamType = F.getArg(*MaskParamPos)->getType(); |
| 281 |
ASSERT_TRUE(MaskParamType->isVectorTy()); |
| 282 |
ASSERT_TRUE( |
| 283 |
cast<VectorType>(MaskParamType)->getElementType()->isIntegerTy(1)); |
| 284 |
} |
| 285 |
|
| 286 |
std::optional<unsigned> VecLenParamPos = |
| 287 |
VPIntrinsic::getVectorLengthParamPos(F.getIntrinsicID()); |
| 288 |
if (VecLenParamPos) { |
| 289 |
Type *VecLenParamType = F.getArg(*VecLenParamPos)->getType(); |
| 290 |
ASSERT_TRUE(VecLenParamType->isIntegerTy(32)); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/// Check that going from Opcode to VP intrinsic and back results in the same |
| 296 |
/// Opcode. |
| 297 |
TEST_F(VPIntrinsicTest, OpcodeRoundTrip) { |
| 298 |
std::vector<unsigned> Opcodes; |
| 299 |
Opcodes.reserve(100); |
| 300 |
|
| 301 |
{ |
| 302 |
#define HANDLE_INST(OCNum, OCName, Class) Opcodes.push_back(OCNum); |
| 303 |
#include "llvm/IR/Instruction.def" |
| 304 |
} |
| 305 |
|
| 306 |
unsigned FullTripCounts = 0; |
| 307 |
for (unsigned OC : Opcodes) { |
| 308 |
Intrinsic::ID VPID = VPIntrinsic::getForOpcode(OC); |
| 309 |
// No equivalent VP intrinsic available. |
| 310 |
if (VPID == Intrinsic::not_intrinsic) |
| 311 |
continue; |
| 312 |
|
| 313 |
std::optional<unsigned> RoundTripOC = |
| 314 |
VPIntrinsic::getFunctionalOpcodeForVP(VPID); |
| 315 |
// No equivalent Opcode available. |
| 316 |
if (!RoundTripOC) |
| 317 |
continue; |
| 318 |
|
| 319 |
ASSERT_EQ(*RoundTripOC, OC); |
| 320 |
++FullTripCounts; |
| 321 |
} |
| 322 |
ASSERT_NE(FullTripCounts, 0u); |
| 323 |
} |
| 324 |
|
| 325 |
/// Check that going from VP intrinsic to Opcode and back results in the same |
| 326 |
/// intrinsic id. |
| 327 |
TEST_F(VPIntrinsicTest, IntrinsicIDRoundTrip) { |
| 328 |
std::unique_ptr<Module> M = createVPDeclarationModule(); |
| 329 |
assert(M); |
| 330 |
|
| 331 |
unsigned FullTripCounts = 0; |
| 332 |
for (const auto &VPDecl : *M) { |
| 333 |
auto VPID = VPDecl.getIntrinsicID(); |
| 334 |
std::optional<unsigned> OC = VPIntrinsic::getFunctionalOpcodeForVP(VPID); |
| 335 |
|
| 336 |
// no equivalent Opcode available |
| 337 |
if (!OC) |
| 338 |
continue; |
| 339 |
|
| 340 |
Intrinsic::ID RoundTripVPID = VPIntrinsic::getForOpcode(*OC); |
| 341 |
|
| 342 |
ASSERT_EQ(RoundTripVPID, VPID); |
| 343 |
++FullTripCounts; |
| 344 |
} |
| 345 |
ASSERT_NE(FullTripCounts, 0u); |
| 346 |
} |
| 347 |
|
| 348 |
/// Check that VPIntrinsic::getDeclarationForParams works. |
| 349 |
TEST_F(VPIntrinsicTest, VPIntrinsicDeclarationForParams) { |
| 350 |
std::unique_ptr<Module> M = createVPDeclarationModule(); |
| 351 |
assert(M); |
| 352 |
|
| 353 |
auto OutM = std::make_unique<Module>("", M->getContext()); |
| 354 |
|
| 355 |
for (auto &F : *M) { |
| 356 |
auto *FuncTy = F.getFunctionType(); |
| 357 |
|
| 358 |
// Declare intrinsic anew with explicit types. |
| 359 |
std::vector<Value *> Values; |
| 360 |
for (auto *ParamTy : FuncTy->params()) |
| 361 |
Values.push_back(UndefValue::get(ParamTy)); |
| 362 |
|
| 363 |
ASSERT_NE(F.getIntrinsicID(), Intrinsic::not_intrinsic); |
| 364 |
auto *NewDecl = VPIntrinsic::getDeclarationForParams( |
| 365 |
OutM.get(), F.getIntrinsicID(), FuncTy->getReturnType(), Values); |
| 366 |
ASSERT_TRUE(NewDecl); |
| 367 |
|
| 368 |
// Check that 'old decl' == 'new decl'. |
| 369 |
ASSERT_EQ(F.getIntrinsicID(), NewDecl->getIntrinsicID()); |
| 370 |
FunctionType::param_iterator ItNewParams = |
| 371 |
NewDecl->getFunctionType()->param_begin(); |
| 372 |
FunctionType::param_iterator EndItNewParams = |
| 373 |
NewDecl->getFunctionType()->param_end(); |
| 374 |
for (auto *ParamTy : FuncTy->params()) { |
| 375 |
ASSERT_NE(ItNewParams, EndItNewParams); |
| 376 |
ASSERT_EQ(*ItNewParams, ParamTy); |
| 377 |
++ItNewParams; |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
/// Check that the HANDLE_VP_TO_CONSTRAINEDFP maps to an existing intrinsic with |
| 383 |
/// the right amount of constrained-fp metadata args. |
| 384 |
TEST_F(VPIntrinsicTest, HandleToConstrainedFP) { |
| 385 |
#define VP_PROPERTY_CONSTRAINEDFP(HASROUND, HASEXCEPT, CFPID) \ |
| 386 |
{ \ |
| 387 |
SmallVector<Intrinsic::IITDescriptor, 5> T; \ |
| 388 |
Intrinsic::getIntrinsicInfoTableEntries(Intrinsic::CFPID, T); \ |
| 389 |
unsigned NumMetadataArgs = 0; \ |
| 390 |
for (auto TD : T) \ |
| 391 |
NumMetadataArgs += (TD.Kind == Intrinsic::IITDescriptor::Metadata); \ |
| 392 |
bool IsCmp = Intrinsic::CFPID == Intrinsic::experimental_constrained_fcmp; \ |
| 393 |
ASSERT_EQ(NumMetadataArgs, (unsigned)(IsCmp + HASROUND + HASEXCEPT)); \ |
| 394 |
} |
| 395 |
#include "llvm/IR/VPIntrinsics.def" |
| 396 |
} |
| 397 |
|
| 398 |
} // end anonymous namespace |
| 399 |
|
| 400 |
/// Check various properties of VPReductionIntrinsics |
| 401 |
TEST_F(VPIntrinsicTest, VPReductions) { |
| 402 |
LLVMContext C; |
| 403 |
SMDiagnostic Err; |
| 404 |
|
| 405 |
std::stringstream Str; |
| 406 |
Str << "declare <8 x i32> @llvm.vp.mul.v8i32(<8 x i32>, <8 x i32>, <8 x i1>, " |
| 407 |
"i32)"; |
| 408 |
for (const char *ReductionOpcode : ReductionIntOpcodes) |
| 409 |
Str << " declare i32 @llvm.vp.reduce." << ReductionOpcode |
| 410 |
<< ".v8i32(i32, <8 x i32>, <8 x i1>, i32) "; |
| 411 |
|
| 412 |
for (const char *ReductionOpcode : ReductionFPOpcodes) |
| 413 |
Str << " declare float @llvm.vp.reduce." << ReductionOpcode |
| 414 |
<< ".v8f32(float, <8 x float>, <8 x i1>, i32) "; |
| 415 |
|
| 416 |
Str << "define void @test_reductions(i32 %start, <8 x i32> %val, float " |
| 417 |
"%fpstart, <8 x float> %fpval, <8 x i1> %m, i32 %vl) {"; |
| 418 |
|
| 419 |
// Mix in a regular non-reduction intrinsic to check that the |
| 420 |
// VPReductionIntrinsic subclass works as intended. |
| 421 |
Str << " %r0 = call <8 x i32> @llvm.vp.mul.v8i32(<8 x i32> %val, <8 x i32> " |
| 422 |
"%val, <8 x i1> %m, i32 %vl)"; |
| 423 |
|
| 424 |
unsigned Idx = 1; |
| 425 |
for (const char *ReductionOpcode : ReductionIntOpcodes) |
| 426 |
Str << " %r" << Idx++ << " = call i32 @llvm.vp.reduce." << ReductionOpcode |
| 427 |
<< ".v8i32(i32 %start, <8 x i32> %val, <8 x i1> %m, i32 %vl)"; |
| 428 |
for (const char *ReductionOpcode : ReductionFPOpcodes) |
| 429 |
Str << " %r" << Idx++ << " = call float @llvm.vp.reduce." |
| 430 |
<< ReductionOpcode |
| 431 |
<< ".v8f32(float %fpstart, <8 x float> %fpval, <8 x i1> %m, i32 %vl)"; |
| 432 |
|
| 433 |
Str << " ret void" |
| 434 |
"}"; |
| 435 |
|
| 436 |
std::unique_ptr<Module> M = parseAssemblyString(Str.str(), Err, C); |
| 437 |
assert(M); |
| 438 |
|
| 439 |
auto *F = M->getFunction("test_reductions"); |
| 440 |
assert(F); |
| 441 |
|
| 442 |
for (const auto &I : F->getEntryBlock()) { |
| 443 |
const VPIntrinsic *VPI = dyn_cast<VPIntrinsic>(&I); |
| 444 |
if (!VPI) |
| 445 |
continue; |
| 446 |
|
| 447 |
Intrinsic::ID ID = VPI->getIntrinsicID(); |
| 448 |
const auto *VPRedI = dyn_cast<VPReductionIntrinsic>(&I); |
| 449 |
|
| 450 |
if (!VPReductionIntrinsic::isVPReduction(ID)) { |
| 451 |
EXPECT_EQ(VPRedI, nullptr); |
| 452 |
EXPECT_EQ(VPReductionIntrinsic::getStartParamPos(ID).has_value(), false); |
| 453 |
EXPECT_EQ(VPReductionIntrinsic::getVectorParamPos(ID).has_value(), false); |
| 454 |
continue; |
| 455 |
} |
| 456 |
|
| 457 |
EXPECT_EQ(VPReductionIntrinsic::getStartParamPos(ID).has_value(), true); |
| 458 |
EXPECT_EQ(VPReductionIntrinsic::getVectorParamPos(ID).has_value(), true); |
| 459 |
ASSERT_NE(VPRedI, nullptr); |
| 460 |
EXPECT_EQ(VPReductionIntrinsic::getStartParamPos(ID), |
| 461 |
VPRedI->getStartParamPos()); |
| 462 |
EXPECT_EQ(VPReductionIntrinsic::getVectorParamPos(ID), |
| 463 |
VPRedI->getVectorParamPos()); |
| 464 |
EXPECT_EQ(VPRedI->getStartParamPos(), 0u); |
| 465 |
EXPECT_EQ(VPRedI->getVectorParamPos(), 1u); |
| 466 |
} |
| 467 |
} |