Skip to content

Commit 78971bc

Browse files
aratajewigcbot
authored andcommitted
Report warning if a global atomic instruction operates on a nullptr
Global atomics with nullptr lead to GPU hang. Generating a warning to avoid time-consuming debugging process.
1 parent c397353 commit 78971bc

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

IGC/Compiler/CISACodeGen/EmitVISAPass.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14507,6 +14507,12 @@ void EmitPass::emitAtomicRaw(llvm::GenIntrinsicInst *pInst, Value *dstAddr,
1450714507
if (pllbuffer->getType()->getPointerAddressSpace() == ADDRESS_SPACE_GLOBAL)
1450814508
{
1450914509
m_currShader->SetHasGlobalAtomics();
14510+
14511+
// Global atomics with nullptr lead to GPU hang. Emit warning to avoid time-consuming debugging process.
14512+
if (resource.m_surfaceType == ESURFACE_STATELESS && isa<llvm::ConstantPointerNull>(pllbuffer))
14513+
{
14514+
m_pCtx->EmitWarning("Possible null pointer dereference! Atomic instruction operates on a nullptr.", pInst);
14515+
}
1451014516
}
1451114517

1451214518
CVariable* pSrc0 = nullptr;
@@ -15300,7 +15306,9 @@ static void divergentBarrierCheck(
1530015306
I->print(SS, true);
1530115307
SS.flush();
1530215308
OS << '\n' << Repr;
15303-
Ctx.EmitError(OS, "Possible divergent barrier found", I);
15309+
OS << "\nerror: ";
15310+
Ctx.EmitMessage(OS, "Possible divergent barrier found", I);
15311+
OS << "\nerror: backend compiler failed build.\n";
1530415312
}
1530515313
}
1530615314
}

IGC/Compiler/CodeGenContext.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,9 @@ namespace IGC
718718
#endif
719719
}
720720

721-
void CodeGenContext::EmitError(std::ostream &OS, const char* errorstr, const llvm::Value* context) const
721+
void CodeGenContext::EmitMessage(std::ostream &OS, const char* messagestr, const llvm::Value* context) const
722722
{
723-
OS << "\nerror: ";
724-
OS << errorstr;
723+
OS << messagestr;
725724
// Try to get debug location to print out the relevant info.
726725
if (const llvm::Instruction *I = llvm::dyn_cast_or_null<llvm::Instruction>(context)) {
727726
if (const llvm::DILocation *DL = I->getDebugLoc()) {
@@ -755,18 +754,19 @@ namespace IGC
755754
}
756755
}
757756
}
758-
OS << "\nerror: backend compiler failed build.\n";
759757
}
760758

761759
void CodeGenContext::EmitError(const char* errorstr, const llvm::Value *context)
762760
{
763-
EmitError(this->oclErrorMessage, errorstr, context);
761+
this->oclErrorMessage << "\nerror: ";
762+
EmitMessage(this->oclErrorMessage, errorstr, context);
763+
this->oclErrorMessage << "\nerror: backend compiler failed build.\n";
764764
}
765765

766-
void CodeGenContext::EmitWarning(const char* warningstr)
766+
void CodeGenContext::EmitWarning(const char* warningstr, const llvm::Value* context)
767767
{
768768
this->oclWarningMessage << "\nwarning: ";
769-
this->oclWarningMessage << warningstr;
769+
EmitMessage(this->oclWarningMessage, warningstr, context);
770770
this->oclWarningMessage << "\n";
771771
}
772772

IGC/Compiler/CodeGenPublic.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,9 +1076,9 @@ namespace IGC
10761076
virtual ~CodeGenContext();
10771077
void clear();
10781078
void clearMD();
1079-
void EmitError(std::ostream &OS, const char* errorstr, const llvm::Value *context) const;
1079+
void EmitMessage(std::ostream &OS, const char* errorstr, const llvm::Value *context) const;
10801080
void EmitError(const char* errorstr, const llvm::Value *context);
1081-
void EmitWarning(const char* warningstr);
1081+
void EmitWarning(const char* warningstr, const llvm::Value *context = nullptr);
10821082
inline bool HasError() const { return !this->oclErrorMessage.str().empty(); }
10831083
inline bool HasWarning() const { return !this->oclWarningMessage.str().empty(); }
10841084
inline const std::string GetWarning() { return this->oclWarningMessage.str(); }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2023 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
// RUN: ocloc compile -file %s -device dg2 -options "-cl-std=CL2.0" 2>&1 | FileCheck %s
10+
11+
// CHECK: warning: Possible null pointer dereference! Atomic instruction operates on a nullptr.
12+
13+
void func(generic int* generic_ptr)
14+
{
15+
global int* global_ptr = to_global(generic_ptr);
16+
atomic_store(global_ptr, 1);
17+
}
18+
19+
kernel void test(local int* ptr)
20+
{
21+
func(ptr);
22+
}

0 commit comments

Comments
 (0)