Skip to content

Commit

Permalink
Merge pull request #211 from frasercrmck/spirv-ll-fix-debug-crashes
Browse files Browse the repository at this point in the history
[spirv-ll] Fix various debug info generation issues
  • Loading branch information
frasercrmck authored Nov 15, 2023
2 parents 9248245 + 1a6d31e commit cf711d0
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 133 deletions.
21 changes: 20 additions & 1 deletion modules/compiler/spirv-ll/include/spirv-ll/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class Builder {
void accessChain(OpCode const &opc);

/// @brief Check if an OpLine range is in progress and end it if there is
void checkEndOpLineRange(bool is_branch = false);
void checkEndOpLineRange();

/// @brief Return a `DIType` object that represents the given type
///
Expand All @@ -335,13 +335,32 @@ class Builder {
/// @return pointer to `DIType` derived from the given `Type`
llvm::DIType *getDIType(llvm::Type *type);

/// @brief Gets (or creates) a DIFile for the given OpLine.
llvm::DIFile *getOrCreateDIFile(const OpLine *op_line);

/// @brief Gets (or creates) a DICompileUnit for the given OpLine.
llvm::DICompileUnit *getOrCreateDICompileUnit(const OpLine *op_line);

/// @brief Gets (or creates) a DISubprogram for the given function and
/// OpLine.
llvm::DISubprogram *getOrCreateDebugFunctionScope(llvm::Function *function,
const OpLine *op_line);

/// @brief Creates the beginning of a line range for the given OpLine,
/// contained within the basic block.
Module::LineRangeBeginTy createLineRangeBegin(const OpLine *op_line,
llvm::BasicBlock &bb);

/// @brief Add debug metadata to the appropriate instructions
void addDebugInfoToModule();

/// @brief Finalizes and adds any metadata to LLVM that was generated by
/// SpvBuilder
void finalizeMetadata();

/// @brief Gets (or creates) the BasicBlock for a spv::Id OpLabel.
llvm::BasicBlock *getOrCreateBasicBlock(spv::Id label);

/// @brief Generates code in a basic block to initialize a builtin variable.
///
/// @param builtin SPIR-V builtin enum denoting which builtin to initialize.
Expand Down
29 changes: 20 additions & 9 deletions modules/compiler/spirv-ll/include/spirv-ll/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,23 @@ class Module : public ModuleHeader {
/// @return The string or an empty one if the ID isn't found.
std::string getDebugString(spv::Id id) const;

/// @brief Set basic block iterator at start of the current OpLine range.
/// @brief A type containing an LLVM debug location and the beginning of the
/// range it corresponds to.
struct LineRangeBeginTy {
const OpLine *op_line;
llvm::DILocation *loc;
llvm::BasicBlock::iterator range_begin = llvm::BasicBlock::iterator();
};

/// @brief Opens up a new OpLine range, setting it to the current one.
///
/// @param location DILocation containing the line/column info.
/// @param iter Basic block iterator at the start of the new range.
void setCurrentOpLineRange(llvm::DILocation *location,
llvm::BasicBlock::iterator iter);
/// Does *not* close the current one. Call closeCurrentOpLineRange() first.
///
/// @param range_begin LineRangeBeginTy information about the range to begin.
void setCurrentOpLineRange(const LineRangeBeginTy &range_begin);

/// @brief Closes (i.e., clears) the current OpLine range.
void closeCurrentOpLineRange();

/// @brief Add a completed OpLine range to the module.
///
Expand All @@ -424,8 +435,7 @@ class Module : public ModuleHeader {
///
/// @return Pair containing `DILocation` and iterator range, location will be
/// nullptr if there isn't an ongoing range
std::pair<llvm::DILocation *, llvm::BasicBlock::iterator>
getCurrentOpLineRange() const;
std::optional<LineRangeBeginTy> getCurrentOpLineRange() const;

/// @brief Add a basic block and associated lexical block to the module.
///
Expand Down Expand Up @@ -1037,8 +1047,9 @@ class Module : public ModuleHeader {
/// translated in its entirety, so we can't construct the `iterator_range`
/// until that's happened but we still need to store the range.
OpLineRangeMap OpLineRanges;
/// @brief DILocation/basic block iterator pair to store current OpLine range.
std::pair<llvm::DILocation *, llvm::BasicBlock::iterator> CurrentOpLineRange;
/// @brief DILocation/basic block iterator pair to store the beginning of the
/// current OpLine range.
std::optional<LineRangeBeginTy> CurrentOpLineRange;
/// @brief Map of BasicBlock to associated `DILexicalBlock`.
llvm::DenseMap<llvm::BasicBlock *, llvm::DILexicalBlock *> LexicalBlocks;
/// @brief Map of function to associated `DISubprogram`.
Expand Down
Loading

0 comments on commit cf711d0

Please sign in to comment.