Skip to content

Commit ec4c755

Browse files
Maetveisigcbot
authored andcommitted
Allow toggling of selective scalarization for igc_opt
Before this change when directly calling the ScalarizeFunction pass, it would always have selective scalarization enabled. The registry flag EnableSelectiveScalarization was only used by callers of createScalarizerPass. Change this so that the pass itself looks at the registry flag. Callers can still override this behavior by explicitly enabling or disabling selective scalarization. All internal users of createScalarizerPass are updated to match their existing behavior.
1 parent 5361ed8 commit ec4c755

File tree

4 files changed

+232
-98
lines changed

4 files changed

+232
-98
lines changed

IGC/AdaptorOCL/UnifyIROCL.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,7 @@ static void CommonOCLBasedPasses(
641641

642642
mpm.add(new ScalarArgAsPointerAnalysis());
643643

644-
// true means selective scalarization
645-
mpm.add(createScalarizerPass(IGC_IS_FLAG_ENABLED(EnableSelectiveScalarizer)));
644+
mpm.add(createScalarizerPass(SelectiveScalarizer::Auto));
646645

647646
// Create a dummy kernel to attach the symbol table if necessary
648647
// Only needed if function pointers, externally linked functions, or relocatable global variables are present

IGC/Compiler/Optimizer/Scalarizer.cpp

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,34 @@ IGC_INITIALIZE_PASS_END(ScalarizeFunction, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG
5757

5858
char ScalarizeFunction::ID = 0;
5959

60-
ScalarizeFunction::ScalarizeFunction(bool selectiveScalarization) : FunctionPass(ID)
60+
ScalarizeFunction::ScalarizeFunction(IGC::SelectiveScalarizer selectiveMode) : FunctionPass(ID)
6161
{
6262
initializeScalarizeFunctionPass(*PassRegistry::getPassRegistry());
6363

6464
for (int i = 0; i < Instruction::OtherOpsEnd; i++) m_transposeCtr[i] = 0;
6565

66-
// Needs IGC_EnableSelectiveScalarizer = 1
67-
m_SelectiveScalarization = selectiveScalarization;
66+
V_PRINT(scalarizer, "ScalarizeFunction constructor\n");
67+
switch(selectiveMode) {
68+
case IGC::SelectiveScalarizer::Off:
69+
V_PRINT(scalarizer, "IGC_EnableSelectiveScalarizer forced off");
70+
m_SelectiveScalarization = false;
71+
break;
72+
case IGC::SelectiveScalarizer::On:
73+
V_PRINT(scalarizer, "IGC_EnableSelectiveScalarizer forced on");
74+
m_SelectiveScalarization = true;
75+
break;
76+
case IGC::SelectiveScalarizer::Auto:
77+
V_PRINT(scalarizer, "IGC_EnableSelectiveScalarizer = ");
78+
V_PRINT(scalarizer, IGC_IS_FLAG_ENABLED(EnableSelectiveScalarizer));
79+
m_SelectiveScalarization = IGC_IS_FLAG_ENABLED(EnableSelectiveScalarizer);
80+
break;
81+
}
82+
V_PRINT(scalarizer, "\n");
6883

6984
// Initialize SCM buffers and allocation
7085
m_SCMAllocationArray = new SCMEntry[ESTIMATED_INST_NUM];
7186
m_SCMArrays.push_back(m_SCMAllocationArray);
7287
m_SCMArrayLocation = 0;
73-
74-
V_PRINT(scalarizer, "ScalarizeFunction constructor\n");
75-
V_PRINT(scalarizer, "IGC_EnableSelectiveScalarizer = ");
76-
V_PRINT(scalarizer, IGC_IS_FLAG_ENABLED(EnableSelectiveScalarizer));
77-
V_PRINT(scalarizer, "\n");
7888
}
7989

8090
bool ScalarizeFunction::doFinalization(llvm::Module& M) {
@@ -244,20 +254,40 @@ void ScalarizeFunction::buildExclusiveSet()
244254
}
245255
else if (BitCastInst* BCI = dyn_cast<BitCastInst>(currInst))
246256
{
257+
auto isBitcastSink = [](BitCastInst *BCI) -> bool {
258+
auto *SrcVTy = dyn_cast<IGCLLVM::FixedVectorType>(
259+
BCI->getOperand(0)->getType());
260+
261+
// If source is not a vector, we don't care about this bitcast
262+
if (!SrcVTy)
263+
return false;
264+
265+
// If destination is a vector then we scalarize if the number of
266+
// elements are the same (elementwise bitcast)
267+
if (auto *DestVTy =
268+
dyn_cast<IGCLLVM::FixedVectorType>(BCI->getType()))
269+
return DestVTy->getNumElements() != SrcVTy->getNumElements();
270+
271+
// If destination is not a vector, we don't want to scalarize
272+
return true;
273+
};
274+
275+
if (isBitcastSink(BCI)) {
247276
workset.push_back(BCI->getOperand(0));
277+
}
248278
}
249279
// try to find a web from the seed
250280
std::set<Value*> defweb;
251281
while (!workset.empty())
252282
{
253-
auto Def = workset.back();
283+
auto* Def = workset.back();
254284
workset.pop_back();
255285
if (m_Excludes.count(Def) || defweb.count(Def))
256286
{
257287
continue;
258288
}
259289

260-
// The web grows "up" through BitCasts and PHI nodes
290+
// The web grows "up" (towards producers) through BitCasts and PHI nodes
261291
// but insert/extract elements and vector shuffles should be scalarized
262292
if (!isAddToWeb(Def)) continue;
263293

@@ -285,7 +315,7 @@ void ScalarizeFunction::buildExclusiveSet()
285315
continue;
286316
}
287317

288-
// The web grows "down" through BitCasts and PHI nodes as well
318+
// The web grows "down" (towards users) through BitCasts and PHI nodes as well
289319
for (auto U : Def->users())
290320
{
291321
if (!defweb.count(U) && isAddToWeb(U))
@@ -1458,8 +1488,8 @@ void ScalarizeFunction::resolveDeferredInstructions()
14581488
m_DRL.clear();
14591489
}
14601490

1461-
extern "C" FunctionPass * createScalarizerPass(bool selectiveScalarization)
1491+
extern "C" FunctionPass * createScalarizerPass(IGC::SelectiveScalarizer selectiveMode)
14621492
{
1463-
return new ScalarizeFunction(selectiveScalarization);
1493+
return new ScalarizeFunction(selectiveMode);
14641494
}
14651495

IGC/Compiler/Optimizer/Scalarizer.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ namespace IGC
3939
// Define estimated amount of instructions in function
4040
#define ESTIMATED_INST_NUM 128
4141

42+
enum class SelectiveScalarizer {
43+
Off,
44+
On,
45+
Auto ///< Based on IGC_EnableSelectiveScalarizer (0 = off, 1 = on)
46+
};
47+
4248
/// @brief Scalarization pass used for converting code in functions
4349
/// which operate on vector types, to work on scalar types (by breaking
4450
/// data elements to scalars, and breaking each vector operation
@@ -51,7 +57,10 @@ namespace IGC
5157
public:
5258
static char ID; // Pass identification, replacement for typeid
5359

54-
ScalarizeFunction(bool selectiveScalarization = true);
60+
// Default value differs from createScalarizerPass to allow control over selective
61+
// scalarization when pass is directly called from the command line (via igc_opt).
62+
ScalarizeFunction(
63+
SelectiveScalarizer selectiveMode = IGC::SelectiveScalarizer::Auto);
5564
ScalarizeFunction(const ScalarizeFunction&) = delete;
5665
ScalarizeFunction& operator=(const ScalarizeFunction&) = delete;
5766

@@ -271,5 +280,5 @@ namespace IGC
271280
/// The ending legs of the web consist of vectorial instructions such as insert and extract elements,
272281
/// vector shuffles, GenISA intrinsics and function calls.
273282
/// The vectorial instructions inside the web consist of bitcasts and PHI nodes.
274-
extern "C" llvm::FunctionPass * createScalarizerPass(bool selectiveScalarization = false);
275-
283+
extern "C" llvm::FunctionPass *createScalarizerPass(
284+
IGC::SelectiveScalarizer selectiveMode = IGC::SelectiveScalarizer::Off);

0 commit comments

Comments
 (0)