Skip to content

Commit

Permalink
[mlir][LLVM] Tidy up DebugTranslation casting
Browse files Browse the repository at this point in the history
Add a specific class for local scope attributes and remove
some unnecessary casts.
  • Loading branch information
River707 committed Jan 20, 2023
1 parent 33ff9d9 commit 05927eb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
10 changes: 10 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMAttrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ class DIScopeAttr : public DINodeAttr {
static bool classof(Attribute attr);
};

/// This class represents a LLVM attribute that describes a local debug info
/// scope.
class DILocalScopeAttr : public DIScopeAttr {
public:
using DIScopeAttr::DIScopeAttr;

/// Support LLVM type casting.
static bool classof(Attribute attr);
};

/// This class represents a LLVM attribute that describes a debug info type.
class DITypeAttr : public DINodeAttr {
public:
Expand Down
11 changes: 9 additions & 2 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@ bool DINodeAttr::classof(Attribute attr) {

bool DIScopeAttr::classof(Attribute attr) {
return llvm::isa<DICompileUnitAttr, DICompositeTypeAttr, DIFileAttr,
DILexicalBlockAttr, DILexicalBlockFileAttr,
DISubprogramAttr>(attr);
DILexicalBlockFileAttr, DILocalScopeAttr>(attr);
}

//===----------------------------------------------------------------------===//
// DILocalScopeAttr
//===----------------------------------------------------------------------===//

bool DILocalScopeAttr::classof(Attribute attr) {
return llvm::isa<DILexicalBlockAttr, DISubprogramAttr>(attr);
}

//===----------------------------------------------------------------------===//
Expand Down
13 changes: 8 additions & 5 deletions mlir/lib/Target/LLVMIR/DebugTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ DebugTranslation::translateImpl(DILexicalBlockFileAttr attr) {
attr.getDiscriminator());
}

llvm::DILocalScope *DebugTranslation::translateImpl(DILocalScopeAttr attr) {
return cast<llvm::DILocalScope>(translate(DINodeAttr(attr)));
}

llvm::DILocalVariable *
DebugTranslation::translateImpl(DILocalVariableAttr attr) {
return llvm::DILocalVariable::get(
Expand Down Expand Up @@ -283,8 +287,8 @@ DebugTranslation::translateLoc(Location loc, llvm::DILocalScope *scope,

// Check for a scope encoded with the location.
if (auto scopedAttr =
fusedLoc.getMetadata().dyn_cast_or_null<LLVM::DIScopeAttr>())
scope = cast<llvm::DILocalScope>(translate(scopedAttr));
fusedLoc.getMetadata().dyn_cast_or_null<LLVM::DILocalScopeAttr>())
scope = translate(scopedAttr);

// For fused locations, merge each of the nodes.
llvmLoc = translateLoc(locations.front(), scope, inlinedAt);
Expand All @@ -294,11 +298,10 @@ DebugTranslation::translateLoc(Location loc, llvm::DILocalScope *scope,
}

} else if (auto nameLoc = loc.dyn_cast<NameLoc>()) {
llvmLoc = translateLoc(loc.cast<NameLoc>().getChildLoc(), scope, inlinedAt);
llvmLoc = translateLoc(nameLoc.getChildLoc(), scope, inlinedAt);

} else if (auto opaqueLoc = loc.dyn_cast<OpaqueLoc>()) {
llvmLoc = translateLoc(loc.cast<OpaqueLoc>().getFallbackLocation(), scope,
inlinedAt);
llvmLoc = translateLoc(opaqueLoc.getFallbackLocation(), scope, inlinedAt);
} else {
llvm_unreachable("unknown location kind");
}
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Target/LLVMIR/DebugTranslation.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class DebugTranslation {
llvm::DIFile *translateImpl(DIFileAttr attr);
llvm::DILexicalBlock *translateImpl(DILexicalBlockAttr attr);
llvm::DILexicalBlockFile *translateImpl(DILexicalBlockFileAttr attr);
llvm::DILocalScope *translateImpl(DILocalScopeAttr attr);
llvm::DILocalVariable *translateImpl(DILocalVariableAttr attr);
llvm::DIScope *translateImpl(DIScopeAttr attr);
llvm::DISubprogram *translateImpl(DISubprogramAttr attr);
Expand Down

0 comments on commit 05927eb

Please sign in to comment.