Skip to content

Commit 61b334b

Browse files
Krebrov001igcbot
authored andcommitted
The dummy values in the m_DRL were previously defined as a load from a null pointer, which is an error.
A load from a null pointer can be a valid code if that pointer occupies a custom target-specific address space. SCMEntry which is a load from a null pointer causes the associated DRLEntry to not be deleted from the m_DRL.
1 parent 7ca8f3b commit 61b334b

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

IGC/Compiler/Optimizer/Scalarizer.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ ScalarizeFunction::~ScalarizeFunction()
8888
{
8989
releaseAllSCMEntries();
9090
delete[] m_SCMAllocationArray;
91+
destroyDummyFunc();
9192
V_PRINT(scalarizer, "ScalarizeFunction destructor\n");
9293
}
9394

@@ -1090,17 +1091,14 @@ void ScalarizeFunction::obtainScalarizedValues(SmallVectorImpl<Value*>& retValue
10901091
// Generate a DRL: dummy values, which will be resolved after all scalarization is complete.
10911092
V_PRINT(scalarizer, "\t\t\t*** Not found. Setting DRL. \n");
10921093
Type* dummyType = origType->getElementType();
1093-
V_PRINT(scalarizer, "\t\tCreate Dummy Scalar value/s (of type " << *dummyType << ")\n");
1094-
Constant* dummyPtr = ConstantPointerNull::get(dummyType->getPointerTo());
1094+
Function* dummy_function = getOrCreateDummyFunc(dummyType);
10951095
DRLEntry newDRLEntry;
10961096
newDRLEntry.unresolvedInst = origValue;
10971097
newDRLEntry.dummyVals.resize(width);
10981098
for (unsigned i = 0; i < width; i++)
10991099
{
1100-
// Generate dummy "load" instruction (but don't really place in function)
1101-
retValues[i + destIdx] = new LoadInst(dummyPtr->getType()->getPointerElementType(),
1102-
dummyPtr, "", false, IGCLLVM::getAlign(1));
1103-
1100+
// Generate dummy "call" instruction (but don't really place in function)
1101+
retValues[i + destIdx] = CallInst::Create(dummy_function);
11041102
newDRLEntry.dummyVals[i] = retValues[i + destIdx];
11051103
}
11061104

@@ -1298,10 +1296,16 @@ void ScalarizeFunction::resolveDeferredInstructions()
12981296
std::map<Value*, Value*> dummyToScalarMap;
12991297

13001298
// lambda to check if a value is a dummy instruction
1301-
auto isDummyValue = [](Value* val)->bool
1299+
auto isDummyValue = [this](Value* val) -> bool
13021300
{
1303-
LoadInst* ld = dyn_cast<LoadInst>(val);
1304-
return (ld && isa<ConstantPointerNull>(ld->getPointerOperand()));
1301+
auto* call = dyn_cast<CallInst>(val);
1302+
if (!call) return false;
1303+
// If the Value is one of the dummy functions that we created.
1304+
for (const auto& function : createdDummyFunctions) {
1305+
if (call->getCalledFunction() == function.second)
1306+
return true;
1307+
}
1308+
return false;
13051309
};
13061310

13071311
for (auto deferredEntry = m_DRL.begin(); m_DRL.size() > 0;)

IGC/Compiler/Optimizer/Scalarizer.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ IN THE SOFTWARE.
4343
#include <string>
4444
#include <sstream>
4545
#include <set>
46+
#include <map>
4647

4748
namespace IGC
4849
{
@@ -193,6 +194,26 @@ namespace IGC
193194
/// @brief release all allocations of SCM entries
194195
void releaseAllSCMEntries();
195196

197+
/// @brief create the dummy function which is called to signify a dummy value
198+
inline llvm::Function* getOrCreateDummyFunc(llvm::Type* dummyType) {
199+
if (createdDummyFunctions.find(dummyType) == createdDummyFunctions.end()) {
200+
llvm::FunctionType* funcType = llvm::FunctionType::get(dummyType, false);
201+
llvm::Function* function = llvm::Function::Create(funcType, llvm::Function::InternalLinkage);
202+
createdDummyFunctions[dummyType] = function;
203+
return function;
204+
}
205+
else
206+
return createdDummyFunctions[dummyType];
207+
}
208+
209+
/// @brief deletes the memory associated with all the created dynamic Function objects in the map
210+
inline void destroyDummyFunc() {
211+
for (auto& function : createdDummyFunctions) {
212+
function.second->deleteValue();
213+
function.second = nullptr;
214+
}
215+
}
216+
196217
/// @brief An array of available SCMEntry's
197218
SCMEntry* m_SCMAllocationArray;
198219

@@ -228,6 +249,9 @@ namespace IGC
228249

229250
/// @brief This holds DataLayout of processed module
230251
const llvm::DataLayout* m_pDL;
252+
253+
/// @brief This holds all the created dummy functions throughout the lifetime of the pass, and manages their memory
254+
std::map<llvm::Type*, llvm::Function*> createdDummyFunctions;
231255
};
232256

233257
} // namespace IGC

0 commit comments

Comments
 (0)