Skip to content

Commit ab2055d

Browse files
PawelJureksys_zuul
authored andcommitted
Fail compilation with error message instead of crash if we can't find sampler argument or inline/global sampler.
Change-Id: Ibcef322ba60863ca0cde02720117e3395b1dc64b
1 parent 7c7e97c commit ab2055d

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

IGC/Compiler/CodeGenContext.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,11 @@ namespace IGC
693693
return;
694694
}
695695

696+
bool CodeGenContext::HasError() const
697+
{
698+
return !this->oclErrorMessage.empty();
699+
}
700+
696701
CompOptions& CodeGenContext::getCompilerOption()
697702
{
698703
return getModuleMetaData()->compOpt;

IGC/Compiler/CodeGenPublic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ namespace IGC
925925
virtual ~CodeGenContext();
926926
void clear();
927927
void EmitError(const char* errorstr);
928+
bool HasError() const;
928929
CompOptions& getCompilerOption();
929930
virtual void resetOnRetry();
930931
virtual uint32_t getNumThreadsPerEU() const;

IGC/Compiler/Optimizer/OCLBIUtils.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,18 @@ using namespace llvm;
3939
using namespace IGC;
4040
using namespace IGC::IGCMD;
4141

42-
void CCommand::execute(CallInst* Inst)
42+
void CCommand::execute(CallInst* Inst, CodeGenContext* CodeGenContext)
4343
{
44-
init(Inst);
44+
init(Inst, CodeGenContext);
4545
createIntrinsic();
4646
}
4747

48-
void CCommand::init(CallInst* Inst)
48+
void CCommand::init(CallInst* Inst, CodeGenContext* CodeGenContext)
4949
{
5050
m_pCallInst = Inst;
5151
m_pFunc = m_pCallInst->getParent()->getParent();
5252
m_pCtx = &(m_pFunc->getContext());
53+
m_pCodeGenContext = CodeGenContext;
5354
m_pFloatType = Type::getFloatTy(*m_pCtx);
5455
m_pIntType = Type::getInt32Ty(*m_pCtx);
5556
m_pFloatZero = Constant::getNullValue(m_pFloatType);
@@ -191,11 +192,11 @@ void CImagesBI::prepareImageBTI()
191192
}
192193
}
193194

194-
void CImagesBI::verifiyCommand(CodeGenContext* context)
195+
void CImagesBI::verifyCommand()
195196
{
196197
if (m_IncorrectBti)
197198
{
198-
context->EmitError("Inconsistent use of image!");
199+
m_pCodeGenContext->EmitError("Inconsistent use of image!");
199200
}
200201
}
201202

@@ -767,6 +768,11 @@ class COCL_sample : public CImagesBI
767768
{
768769
ConstantInt* samplerIndex = nullptr;
769770
Value* samplerParam = CImagesBI::CImagesUtils::traceImageOrSamplerArgument(m_pCallInst, 1, m_pMdUtils, m_modMD);
771+
if (!samplerParam) {
772+
m_pCodeGenContext->EmitError("There are instructions that use a sampler, but no sampler found in the kernel!");
773+
return nullptr;
774+
}
775+
770776
// Argument samplers are looked up in the parameter map
771777
if (isa<Argument>(samplerParam))
772778
{
@@ -961,12 +967,14 @@ class COCL_sample : public CImagesBI
961967
}
962968
}
963969

964-
void prepareSamplerIndex()
970+
bool prepareSamplerIndex()
965971
{
966972
ConstantInt* samplerIndex = getSamplerIndex();
973+
if (!samplerIndex) return false;
967974
unsigned int addrSpace = EncodeAS4GFXResource(*samplerIndex, SAMPLER, 0);
968975
Value* sampler = ConstantPointerNull::get(PointerType::get(samplerIndex->getType(), addrSpace));
969976
m_args.push_back(sampler);
977+
return true;
970978
}
971979

972980
void prepareGradients(Dimension Dim, Value* gradX, Value* gradY)
@@ -1057,7 +1065,9 @@ class COCL_sample_l : public COCL_sample
10571065
m_args.push_back(CoordZ);
10581066
m_args.push_back(m_pFloatZero); // ai (?)
10591067
createGetBufferPtr();
1060-
prepareSamplerIndex();
1068+
bool samplerIndexFound = prepareSamplerIndex();
1069+
if (!samplerIndexFound) return;
1070+
10611071
prepareZeroOffsets();
10621072
Type* types[] = { m_pCallInst->getType(), m_pFloatType, m_args[5]->getType(), m_args[6]->getType() };
10631073
replaceGenISACallInst(GenISAIntrinsic::GenISA_sampleLptr, types);
@@ -1936,8 +1946,8 @@ bool CBuiltinsResolver::resolveBI(CallInst* Inst)
19361946
{
19371947
return false;
19381948
}
1939-
m_CommandMap[calleeName]->execute(Inst);
1940-
m_CommandMap[calleeName]->verifiyCommand(m_CodeGenContext);
1949+
m_CommandMap[calleeName]->execute(Inst, m_CodeGenContext);
1950+
m_CommandMap[calleeName]->verifyCommand();
19411951

1942-
return true;
1952+
return !m_CodeGenContext->HasError();
19431953
}

IGC/Compiler/Optimizer/OCLBIUtils.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,22 @@ namespace IGC
7272
void replaceGenISACallInst(llvm::GenISAIntrinsic::ID intrinsicName, llvm::ArrayRef<llvm::Type*> Tys = llvm::None);
7373

7474
/// @brief reap init() and createIntrinsic().
75-
/// @param Inst the call instruction that need to be replaced.
76-
void execute(llvm::CallInst* Inst);
75+
/// @param Inst the call instruction that need to be replaced.
76+
/// @param CodeGenContext context to return errors
77+
void execute(llvm::CallInst* Inst, IGC::CodeGenContext* CodeGenContext);
7778

7879
/// @brief replace the old __builtin_IB function call with the match llvm/GenISA_ISA
7980
/// instructions.
8081
/// @param Inst the call instruction that need to be replaced.
8182
virtual void createIntrinsic() = 0;
8283

8384
/// @brief verify that there are no user errors
84-
/// @param context to return errors
85-
virtual void verifiyCommand(IGC::CodeGenContext*) {}
85+
virtual void verifyCommand() {}
8686

8787
/// @brief initialize the callInst, function, context, constants and clear the arg list.
88-
/// @param Inst the call instruction that need to be replaced.
89-
void init(llvm::CallInst* Inst);
88+
/// @param Inst the call instruction that need to be replaced.
89+
/// @param CodeGenContext context to return errors
90+
void init(llvm::CallInst* Inst, IGC::CodeGenContext* CodeGenContext);
9091
void clearArgsList(void);
9192

9293
enum CoordType
@@ -102,6 +103,7 @@ namespace IGC
102103
m_pCallInst(nullptr),
103104
m_pFunc(nullptr),
104105
m_pCtx(nullptr),
106+
m_pCodeGenContext(nullptr),
105107
m_pFloatType(nullptr),
106108
m_pIntType(nullptr)
107109
{}
@@ -114,6 +116,7 @@ namespace IGC
114116
llvm::CallInst* m_pCallInst;
115117
llvm::Function* m_pFunc;
116118
llvm::LLVMContext* m_pCtx;
119+
IGC::CodeGenContext* m_pCodeGenContext;
117120
llvm::SmallVector<llvm::Value*, 10> m_args;
118121
llvm::DebugLoc m_DL;
119122
llvm::Type* m_pFloatType;
@@ -231,7 +234,7 @@ namespace IGC
231234
/// @brief returns "true" if the "val" is integer or float with fractional part = 0.
232235
static bool derivedFromInt(const llvm::Value* pVal);
233236

234-
void verifiyCommand(IGC::CodeGenContext*);
237+
void verifyCommand();
235238

236239
protected:
237240
/// @brief push the image index into the function argument list

0 commit comments

Comments
 (0)