Skip to content

Commit 9523a0a

Browse files
Addressing reviewers.
1 parent 0daec94 commit 9523a0a

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

llvm/lib/CodeGen/ReplaceWithVeclib.cpp

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// Replaces instructions to LLVM vector intrinsics (i.e., the frem instruction
10-
// or calls to LLVM intrinsics with vector operands) with matching calls to
11-
// functions from a vector library (e.g., libmvec, SVML) according to
12-
// TargetLibraryInfo.
9+
// Replaces LLVM IR instructions with vector operands (i.e., the frem
10+
// instruction or calls to LLVM intrinsics) with matching calls to functions
11+
// from a vector library (e.g libmvec, SVML) using TargetLibraryInfo interface
1312
//
1413
//===----------------------------------------------------------------------===//
1514

@@ -70,7 +69,7 @@ Function *getTLIFunction(Module *M, FunctionType *VectorFTy,
7069
return TLIFunc;
7170
}
7271

73-
/// Replace the Instruction \p I with a call to the corresponding function from
72+
/// Replace the instruction \p I with a call to the corresponding function from
7473
/// the vector library ( \p TLIVecFunc ).
7574
static void replaceWithTLIFunction(Instruction &I, VFInfo &Info,
7675
Function *TLIVecFunc) {
@@ -84,7 +83,7 @@ static void replaceWithTLIFunction(Instruction &I, VFInfo &Info,
8483
Constant::getAllOnesValue(MaskTy));
8584
}
8685

87-
// Preserve the operand bundles for CallInsts.
86+
// If it is a call instruction, preserve the operand bundles.
8887
SmallVector<OperandBundleDef, 1> OpBundles;
8988
if (CI)
9089
CI->getOperandBundlesAsDefs(OpBundles);
@@ -98,65 +97,63 @@ static void replaceWithTLIFunction(Instruction &I, VFInfo &Info,
9897

9998
/// Returns true when successfully replaced \p I with a suitable function taking
10099
/// vector arguments, based on available mappings in the \p TLI. Currently only
101-
/// works when \p I is a call to vectorized intrinsic or the frem Instruction.
100+
/// works when \p I is a call to vectorized intrinsic or the frem instruction.
102101
static bool replaceWithCallToVeclib(const TargetLibraryInfo &TLI,
103102
Instruction &I) {
104103
std::string ScalarName;
105-
ElementCount VF = ElementCount::getFixed(0);
104+
ElementCount EC = ElementCount::getFixed(0);
106105
CallInst *CI = dyn_cast<CallInst>(&I);
107106
SmallVector<Type *, 8> ScalarArgTypes;
107+
// Compute the argument types of the corresponding scalar call, the scalar
108+
// function name, and EC. For CI, it additionally checks if in the vector
109+
// call, all vector operands have the same EC.
108110
if (CI) {
109111
Intrinsic::ID IID = Intrinsic::not_intrinsic;
110112
IID = CI->getCalledFunction()->getIntrinsicID();
111-
// Compute arguments types of the corresponding scalar call. Additionally
112-
// checks if in the vector call, all vector operands have the same EC.
113-
for (auto Arg : enumerate(CI ? CI->args() : I.operands())) {
113+
for (auto Arg : enumerate(CI->args())) {
114114
auto *ArgTy = Arg.value()->getType();
115-
if (CI && isVectorIntrinsicWithScalarOpAtArg(IID, Arg.index())) {
115+
if (isVectorIntrinsicWithScalarOpAtArg(IID, Arg.index())) {
116116
ScalarArgTypes.push_back(ArgTy);
117-
} else {
118-
auto *VectorArgTy = dyn_cast<VectorType>(ArgTy);
119-
// We are expecting only VectorTypes, as:
120-
// - with a CallInst, scalar operands are handled earlier
121-
// - with the frem Instruction, both operands must be vectors.
122-
if (!VectorArgTy)
123-
return false;
124-
ScalarArgTypes.push_back(ArgTy->getScalarType());
117+
} else if (auto *VectorArgTy = dyn_cast<VectorType>(ArgTy)) {
118+
ScalarArgTypes.push_back(VectorArgTy->getElementType());
125119
// Disallow vector arguments with different VFs. When processing the
126120
// first vector argument, store it's VF, and for the rest ensure that
127121
// they match it.
128-
if (VF.isZero())
129-
VF = VectorArgTy->getElementCount();
130-
else if (VF != VectorArgTy->getElementCount())
122+
if (EC.isZero())
123+
EC = VectorArgTy->getElementCount();
124+
else if (EC != VectorArgTy->getElementCount())
131125
return false;
132-
}
126+
} else
127+
// Exit when it is supposed to be a vector argument but it isn't.
128+
return false;
133129
}
134130
// Try to reconstruct the name for the scalar version of the instruction,
135131
// using scalar argument types.
136132
ScalarName = Intrinsic::isOverloaded(IID)
137133
? Intrinsic::getName(IID, ScalarArgTypes, I.getModule())
138134
: Intrinsic::getName(IID).str();
139135
} else {
136+
assert(I.getType()->isVectorTy() && "Instruction must use vectors");
140137
LibFunc Func;
141138
auto *ScalarTy = I.getType()->getScalarType();
142139
if (!TLI.getLibFunc(I.getOpcode(), ScalarTy, Func))
143140
return false;
144141
ScalarName = TLI.getName(Func);
145142
ScalarArgTypes = {ScalarTy, ScalarTy};
146143
if (auto *VTy = dyn_cast<VectorType>(I.getType()))
147-
VF = VTy->getElementCount();
144+
EC = VTy->getElementCount();
148145
}
149146

150147
// Try to find the mapping for the scalar version of this intrinsic and the
151148
// exact vector width of the call operands in the TargetLibraryInfo. First,
152149
// check with a non-masked variant, and if that fails try with a masked one.
153150
const VecDesc *VD =
154-
TLI.getVectorMappingInfo(ScalarName, VF, /*Masked*/ false);
155-
if (!VD && !(VD = TLI.getVectorMappingInfo(ScalarName, VF, /*Masked*/ true)))
151+
TLI.getVectorMappingInfo(ScalarName, EC, /*Masked*/ false);
152+
if (!VD && !(VD = TLI.getVectorMappingInfo(ScalarName, EC, /*Masked*/ true)))
156153
return false;
157154

158155
LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Found TLI mapping from: `" << ScalarName
159-
<< "` and vector width " << VF << " to: `"
156+
<< "` and vector width " << EC << " to: `"
160157
<< VD->getVectorFnName() << "`.\n");
161158

162159
// Replace the call to the intrinsic with a call to the vector library
@@ -183,16 +180,16 @@ static bool replaceWithCallToVeclib(const TargetLibraryInfo &TLI,
183180
return true;
184181
}
185182

186-
/// Supported Instructions \p I are either frem or CallInsts to Intrinsics.
183+
/// Supported instructions \p I are either frem or CallInsts to intrinsics.
187184
static bool isSupportedInstruction(Instruction *I) {
188185
if (auto *CI = dyn_cast<CallInst>(I)) {
189-
if (!CI->getCalledFunction() ||
190-
CI->getCalledFunction()->getIntrinsicID() == Intrinsic::not_intrinsic)
191-
return false;
192-
} else if (I->getOpcode() != Instruction::FRem)
193-
return false;
186+
if (CI->getCalledFunction() &&
187+
CI->getCalledFunction()->getIntrinsicID() != Intrinsic::not_intrinsic)
188+
return true;
189+
} else if (I->getOpcode() == Instruction::FRem && I->getType()->isVectorTy())
190+
return true;
194191

195-
return true;
192+
return false;
196193
}
197194

198195
static bool runImpl(const TargetLibraryInfo &TLI, Function &F) {

0 commit comments

Comments
 (0)