Skip to content

Commit 3c8197d

Browse files
authored
[spirv] Improve lines and columns for DebugLine (pt 1) (#4071)
In particular, add support for start and end lines and columns for DebugLine. Previously, end line and column was same as start line and column. This is just the first installment of this change. It covers binary ops, OpAccessChain, OpLoad, OpStore, DebugDeclare, OpSample and OpReturn. A second commit will complete support. This commit primarily adds SourceRange to the appropriate emit and generation functions. This is necessary since sometimes neither value in the SourceRange matches the accompanying SourceLocation. Note that while the end column is more correct than it was previously, it is not perfectly correct. It currently points to the beginning of the last token rather than the end of that token. We will attempt to improve this with a followup commit.
1 parent cb7a696 commit 3c8197d

10 files changed

Lines changed: 241 additions & 161 deletions

File tree

tools/clang/include/clang/SPIRV/SpirvBuilder.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ class SpirvBuilder {
173173
/// <result-type> from the given pointer. Returns the instruction pointer for
174174
/// the loaded value.
175175
SpirvLoad *createLoad(QualType resultType, SpirvInstruction *pointer,
176-
SourceLocation loc);
176+
SourceLocation loc, SourceRange range = {});
177177
SpirvLoad *createLoad(const SpirvType *resultType, SpirvInstruction *pointer,
178-
SourceLocation loc);
178+
SourceLocation loc, SourceRange range = {});
179179

180180
/// \brief Creates an OpCopyObject instruction from the given pointer.
181181
SpirvCopyObject *createCopyObject(QualType resultType,
@@ -184,7 +184,7 @@ class SpirvBuilder {
184184
/// \brief Creates a store instruction storing the given value into the given
185185
/// address.
186186
void createStore(SpirvInstruction *address, SpirvInstruction *value,
187-
SourceLocation loc);
187+
SourceLocation loc, SourceRange range = {});
188188

189189
/// \brief Creates a function call instruction and returns the instruction
190190
/// pointer for the return value.
@@ -202,7 +202,7 @@ class SpirvBuilder {
202202
SpirvAccessChain *
203203
createAccessChain(QualType resultType, SpirvInstruction *base,
204204
llvm::ArrayRef<SpirvInstruction *> indexes,
205-
SourceLocation loc);
205+
SourceLocation loc, SourceRange range = {});
206206
SpirvAccessChain *
207207
createAccessChain(const SpirvType *resultType, SpirvInstruction *base,
208208
llvm::ArrayRef<SpirvInstruction *> indexes,
@@ -217,7 +217,7 @@ class SpirvBuilder {
217217
/// the instruction pointer for the result.
218218
SpirvBinaryOp *createBinaryOp(spv::Op op, QualType resultType,
219219
SpirvInstruction *lhs, SpirvInstruction *rhs,
220-
SourceLocation loc);
220+
SourceLocation loc, SourceRange range = {});
221221

222222
SpirvSpecConstantBinaryOp *createSpecConstantBinaryOp(spv::Op op,
223223
QualType resultType,
@@ -257,7 +257,8 @@ class SpirvBuilder {
257257
/// decorations for the given parameters.
258258
SpirvSampledImage *createSampledImage(QualType, SpirvInstruction *image,
259259
SpirvInstruction *sampler,
260-
SourceLocation);
260+
SourceLocation loc,
261+
SourceRange range = {});
261262

262263
/// \brief Creates an OpImageTexelPointer SPIR-V instruction with the given
263264
/// parameters.
@@ -295,7 +296,7 @@ class SpirvBuilder {
295296
SpirvInstruction *constOffset, SpirvInstruction *varOffset,
296297
SpirvInstruction *constOffsets, SpirvInstruction *sample,
297298
SpirvInstruction *minLod, SpirvInstruction *residencyCodeId,
298-
SourceLocation loc);
299+
SourceLocation loc, SourceRange range = {});
299300

300301
/// \brief Creates SPIR-V instructions for reading a texel from an image. If
301302
/// doImageFetch is true, OpImageFetch is used. OpImageRead is used otherwise.
@@ -335,7 +336,7 @@ class SpirvBuilder {
335336
SpirvInstruction *compareVal, SpirvInstruction *constOffset,
336337
SpirvInstruction *varOffset, SpirvInstruction *constOffsets,
337338
SpirvInstruction *sample, SpirvInstruction *residencyCode,
338-
SourceLocation);
339+
SourceLocation loc, SourceRange range = {});
339340

340341
/// \brief Creates an OpImageSparseTexelsResident SPIR-V instruction for the
341342
/// given Resident Code and returns the instruction pointer.
@@ -393,9 +394,10 @@ class SpirvBuilder {
393394
spv::LoopControlMask loopControl = spv::LoopControlMask::MaskNone);
394395

395396
/// \brief Creates a return instruction.
396-
void createReturn(SourceLocation);
397+
void createReturn(SourceLocation, SourceRange range = {});
397398
/// \brief Creates a return value instruction.
398-
void createReturnValue(SpirvInstruction *value, SourceLocation);
399+
void createReturnValue(SpirvInstruction *value, SourceLocation,
400+
SourceRange range = {});
399401

400402
/// \brief Creates an OpExtInst instruction for the GLSL extended instruction
401403
/// set, with the given instruction number, and operands. Returns the
@@ -486,6 +488,7 @@ class SpirvBuilder {
486488

487489
SpirvDebugDeclare *createDebugDeclare(
488490
SpirvDebugLocalVariable *dbgVar, SpirvInstruction *var,
491+
SourceLocation loc = {}, SourceRange range = {},
489492
llvm::Optional<SpirvDebugExpression *> dbgExpr = llvm::None);
490493

491494
SpirvDebugFunction *
@@ -668,7 +671,7 @@ class SpirvBuilder {
668671
/// \brief Decorates the given target with the given string.
669672
void decorateString(SpirvInstruction *target, unsigned decorate,
670673
llvm::StringRef strLiteral,
671-
llvm::Optional<uint32_t> memberIdx = llvm::None);
674+
llvm::Optional<uint32_t> memberIdx = llvm::None);
672675

673676
/// --- Constants ---
674677
/// Each of these methods can acquire a unique constant from the SpirvContext,

tools/clang/include/clang/SPIRV/SpirvInstruction.h

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class SpirvInstruction {
183183
void setResultId(uint32_t id) { resultId = id; }
184184

185185
clang::SourceLocation getSourceLocation() const { return srcLoc; }
186+
clang::SourceRange getSourceRange() const { return srcRange; }
186187

187188
void setDebugName(llvm::StringRef name) { debugName = name; }
188189
llvm::StringRef getDebugName() const { return debugName; }
@@ -219,7 +220,7 @@ class SpirvInstruction {
219220
protected:
220221
// Forbid creating SpirvInstruction directly
221222
SpirvInstruction(Kind kind, spv::Op opcode, QualType astResultType,
222-
SourceLocation loc);
223+
SourceLocation loc, SourceRange range = {});
223224

224225
protected:
225226
const Kind kind;
@@ -228,6 +229,7 @@ class SpirvInstruction {
228229
QualType astResultType;
229230
uint32_t resultId;
230231
SourceLocation srcLoc;
232+
SourceRange srcRange;
231233
std::string debugName;
232234
const SpirvType *resultType;
233235
uint32_t resultTypeId;
@@ -659,7 +661,8 @@ class SpirvTerminator : public SpirvInstruction {
659661
}
660662

661663
protected:
662-
SpirvTerminator(Kind kind, spv::Op opcode, SourceLocation loc);
664+
SpirvTerminator(Kind kind, spv::Op opcode, SourceLocation loc,
665+
SourceRange range = {});
663666
};
664667

665668
/// \brief Base class for branching instructions
@@ -751,7 +754,8 @@ class SpirvKill : public SpirvTerminator {
751754
/// \brief OpReturn and OpReturnValue instructions
752755
class SpirvReturn : public SpirvTerminator {
753756
public:
754-
SpirvReturn(SourceLocation loc, SpirvInstruction *retVal = 0);
757+
SpirvReturn(SourceLocation loc, SpirvInstruction *retVal = 0,
758+
SourceRange range = {});
755759

756760
DEFINE_RELEASE_MEMORY_FOR_CLASS(SpirvReturn)
757761

@@ -825,7 +829,8 @@ class SpirvAccessChain : public SpirvInstruction {
825829
public:
826830
SpirvAccessChain(QualType resultType, SourceLocation loc,
827831
SpirvInstruction *base,
828-
llvm::ArrayRef<SpirvInstruction *> indexVec);
832+
llvm::ArrayRef<SpirvInstruction *> indexVec,
833+
SourceRange range = {});
829834

830835
DEFINE_RELEASE_MEMORY_FOR_CLASS(SpirvAccessChain)
831836

@@ -1002,7 +1007,8 @@ class SpirvBarrier : public SpirvInstruction {
10021007
class SpirvBinaryOp : public SpirvInstruction {
10031008
public:
10041009
SpirvBinaryOp(spv::Op opcode, QualType resultType, SourceLocation loc,
1005-
SpirvInstruction *op1, SpirvInstruction *op2);
1010+
SpirvInstruction *op1, SpirvInstruction *op2,
1011+
SourceRange range = {});
10061012

10071013
DEFINE_RELEASE_MEMORY_FOR_CLASS(SpirvBinaryOp)
10081014

@@ -1481,20 +1487,18 @@ class SpirvNonUniformUnaryOp : public SpirvGroupNonUniformOp {
14811487
///
14821488
class SpirvImageOp : public SpirvInstruction {
14831489
public:
1484-
SpirvImageOp(spv::Op opcode, QualType resultType, SourceLocation loc,
1485-
SpirvInstruction *image, SpirvInstruction *coordinate,
1486-
spv::ImageOperandsMask mask, SpirvInstruction *dref = nullptr,
1487-
SpirvInstruction *bias = nullptr,
1488-
SpirvInstruction *lod = nullptr,
1489-
SpirvInstruction *gradDx = nullptr,
1490-
SpirvInstruction *gradDy = nullptr,
1491-
SpirvInstruction *constOffset = nullptr,
1492-
SpirvInstruction *offset = nullptr,
1493-
SpirvInstruction *constOffsets = nullptr,
1494-
SpirvInstruction *sample = nullptr,
1495-
SpirvInstruction *minLod = nullptr,
1496-
SpirvInstruction *component = nullptr,
1497-
SpirvInstruction *texelToWrite = nullptr);
1490+
SpirvImageOp(
1491+
spv::Op opcode, QualType resultType, SourceLocation loc,
1492+
SpirvInstruction *image, SpirvInstruction *coordinate,
1493+
spv::ImageOperandsMask mask, SpirvInstruction *dref = nullptr,
1494+
SpirvInstruction *bias = nullptr, SpirvInstruction *lod = nullptr,
1495+
SpirvInstruction *gradDx = nullptr, SpirvInstruction *gradDy = nullptr,
1496+
SpirvInstruction *constOffset = nullptr,
1497+
SpirvInstruction *offset = nullptr,
1498+
SpirvInstruction *constOffsets = nullptr,
1499+
SpirvInstruction *sample = nullptr, SpirvInstruction *minLod = nullptr,
1500+
SpirvInstruction *component = nullptr,
1501+
SpirvInstruction *texelToWrite = nullptr, SourceRange range = {});
14981502

14991503
DEFINE_RELEASE_MEMORY_FOR_CLASS(SpirvImageOp)
15001504

@@ -1647,6 +1651,7 @@ class SpirvImageTexelPointer : public SpirvInstruction {
16471651
class SpirvLoad : public SpirvInstruction {
16481652
public:
16491653
SpirvLoad(QualType resultType, SourceLocation loc, SpirvInstruction *pointer,
1654+
SourceRange range = {},
16501655
llvm::Optional<spv::MemoryAccessMask> mask = llvm::None);
16511656

16521657
DEFINE_RELEASE_MEMORY_FOR_CLASS(SpirvLoad)
@@ -1697,7 +1702,8 @@ class SpirvCopyObject : public SpirvInstruction {
16971702
class SpirvSampledImage : public SpirvInstruction {
16981703
public:
16991704
SpirvSampledImage(QualType resultType, SourceLocation loc,
1700-
SpirvInstruction *image, SpirvInstruction *sampler);
1705+
SpirvInstruction *image, SpirvInstruction *sampler,
1706+
SourceRange range = {});
17011707

17021708
DEFINE_RELEASE_MEMORY_FOR_CLASS(SpirvSampledImage)
17031709

@@ -1795,7 +1801,8 @@ class SpirvStore : public SpirvInstruction {
17951801
public:
17961802
SpirvStore(SourceLocation loc, SpirvInstruction *pointer,
17971803
SpirvInstruction *object,
1798-
llvm::Optional<spv::MemoryAccessMask> mask = llvm::None);
1804+
llvm::Optional<spv::MemoryAccessMask> mask = llvm::None,
1805+
SourceRange range = {});
17991806

18001807
DEFINE_RELEASE_MEMORY_FOR_CLASS(SpirvStore)
18011808

@@ -2383,7 +2390,8 @@ class SpirvDebugExpression : public SpirvDebugInstruction {
23832390
class SpirvDebugDeclare : public SpirvDebugInstruction {
23842391
public:
23852392
SpirvDebugDeclare(SpirvDebugLocalVariable *, SpirvInstruction *,
2386-
SpirvDebugExpression *);
2393+
SpirvDebugExpression *, SourceLocation loc = {},
2394+
SourceRange range = {});
23872395

23882396
DEFINE_RELEASE_MEMORY_FOR_CLASS(SpirvDebugDeclare)
23892397

tools/clang/lib/SPIRV/DeclResultIdMapper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ DeclResultIdMapper::createFnParam(const ParmVarDecl *param,
840840
uint32_t dbgArgNumber) {
841841
const auto type = getTypeOrFnRetType(param);
842842
const auto loc = param->getLocation();
843+
const auto range = param->getSourceRange();
843844
const auto name = param->getName();
844845
SpirvFunctionParameter *fnParamInstr = spvBuilder.addFnParam(
845846
type, param->hasAttr<HLSLPreciseAttr>(), loc, param->getName());
@@ -861,7 +862,7 @@ DeclResultIdMapper::createFnParam(const ParmVarDecl *param,
861862
auto *debugLocalVar = spvBuilder.createDebugLocalVariable(
862863
type, name, info->source, line, column, info->scopeStack.back(), flags,
863864
dbgArgNumber);
864-
spvBuilder.createDebugDeclare(debugLocalVar, fnParamInstr);
865+
spvBuilder.createDebugDeclare(debugLocalVar, fnParamInstr, loc, range);
865866
}
866867

867868
return fnParamInstr;

0 commit comments

Comments
 (0)