Skip to content

Commit b83caa3

Browse files
committed
[mlir][llvm] Add support for loop metadata import
This commit introduces functionality to import loop metadata. Loop metadata nodes are transformed into LoopAnnotationAttrs and attached to the corresponding branch operations. Reviewed By: gysit Differential Revision: https://reviews.llvm.org/D143376
1 parent 09216cf commit b83caa3

File tree

8 files changed

+951
-20
lines changed

8 files changed

+951
-20
lines changed

mlir/include/mlir/Target/LLVMIR/ModuleImport.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace LLVM {
3333

3434
namespace detail {
3535
class DebugImporter;
36+
class LoopAnnotationImporter;
3637
} // namespace detail
3738

3839
/// Module import implementation class that provides methods to import globals
@@ -164,11 +165,16 @@ class ModuleImport {
164165
return tbaaMapping.lookup(node);
165166
}
166167

167-
/// Returns the MLIR symbol reference mapped to the given LLVM access
168-
/// group metadata `node`.
169-
SymbolRefAttr lookupAccessGroupAttr(const llvm::MDNode *node) const {
170-
return accessGroupMapping.lookup(node);
171-
}
168+
/// Returns the symbol references pointing to the access group operations that
169+
/// map to the access group nodes starting from the access group metadata
170+
/// `node`. Returns failure, if any of the symbol references cannot be found.
171+
FailureOr<SmallVector<SymbolRefAttr>>
172+
lookupAccessGroupAttrs(const llvm::MDNode *node) const;
173+
174+
/// Returns the loop annotation attribute that corresponds to the given LLVM
175+
/// loop metadata `node`.
176+
LoopAnnotationAttr translateLoopAnnotationAttr(const llvm::MDNode *node,
177+
Location loc) const;
172178

173179
private:
174180
/// Clears the block and value mapping before processing a new region.
@@ -303,6 +309,8 @@ class ModuleImport {
303309
LLVM::TypeFromLLVMIRTranslator typeTranslator;
304310
/// Stateful debug information importer.
305311
std::unique_ptr<detail::DebugImporter> debugImporter;
312+
/// Loop annotation importer.
313+
std::unique_ptr<detail::LoopAnnotationImporter> loopAnnotationImporter;
306314
};
307315

308316
} // namespace LLVM

mlir/lib/Target/LLVMIR/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LLVM_OPTIONAL_SOURCES
55
ConvertToLLVMIR.cpp
66
DebugTranslation.cpp
77
DebugImporter.cpp
8+
LoopAnnotationImporter.cpp
89
LoopAnnotationTranslation.cpp
910
ModuleTranslation.cpp
1011
ModuleImport.cpp
@@ -55,6 +56,7 @@ add_mlir_translation_library(MLIRToLLVMIRTranslationRegistration
5556

5657
add_mlir_translation_library(MLIRTargetLLVMIRImport
5758
DebugImporter.cpp
59+
LoopAnnotationImporter.cpp
5860
ModuleImport.cpp
5961
TypeFromLLVM.cpp
6062

mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder,
7272
static ArrayRef<unsigned> getSupportedMetadataImpl() {
7373
static const SmallVector<unsigned> convertibleMetadata = {
7474
llvm::LLVMContext::MD_prof, llvm::LLVMContext::MD_tbaa,
75-
llvm::LLVMContext::MD_access_group};
75+
llvm::LLVMContext::MD_access_group, llvm::LLVMContext::MD_loop};
7676
return convertibleMetadata;
7777
}
7878

@@ -141,28 +141,38 @@ static LogicalResult setTBAAAttr(const llvm::MDNode *node, Operation *op,
141141
return success();
142142
}
143143

144-
/// Searches the symbol references pointing to the access group operations that
145-
/// map to the access group nodes starting from the access group metadata
144+
/// Looks up all the symbol references pointing to the access group operations
145+
/// that map to the access group nodes starting from the access group metadata
146146
/// `node`, and attaches all of them to the imported operation if the lookups
147147
/// succeed. Returns failure otherwise.
148148
static LogicalResult setAccessGroupAttr(const llvm::MDNode *node, Operation *op,
149149
LLVM::ModuleImport &moduleImport) {
150-
// An access group node is either access group or an access group list.
151-
SmallVector<Attribute> accessGroups;
152-
if (!node->getNumOperands())
153-
accessGroups.push_back(moduleImport.lookupAccessGroupAttr(node));
154-
for (const llvm::MDOperand &operand : node->operands()) {
155-
auto *node = cast<llvm::MDNode>(operand.get());
156-
accessGroups.push_back(moduleImport.lookupAccessGroupAttr(node));
157-
}
158-
// Exit if one of the access group node lookups failed.
159-
if (llvm::is_contained(accessGroups, nullptr))
150+
FailureOr<SmallVector<SymbolRefAttr>> accessGroups =
151+
moduleImport.lookupAccessGroupAttrs(node);
152+
if (failed(accessGroups))
160153
return failure();
161154

155+
SmallVector<Attribute> accessGroupAttrs(accessGroups->begin(),
156+
accessGroups->end());
162157
op->setAttr(LLVMDialect::getAccessGroupsAttrName(),
163-
ArrayAttr::get(op->getContext(), accessGroups));
158+
ArrayAttr::get(op->getContext(), accessGroupAttrs));
164159
return success();
165160
}
161+
162+
/// Converts the given loop metadata node to an MLIR loop annotation attribute
163+
/// and attaches it to the imported operation if the translation succeeds.
164+
/// Returns failure otherwise.
165+
static LogicalResult setLoopAttr(const llvm::MDNode *node, Operation *op,
166+
LLVM::ModuleImport &moduleImport) {
167+
LoopAnnotationAttr attr =
168+
moduleImport.translateLoopAnnotationAttr(node, op->getLoc());
169+
if (!attr)
170+
return failure();
171+
172+
op->setAttr(LLVMDialect::getLoopAttrName(), attr);
173+
return success();
174+
}
175+
166176
namespace {
167177

168178
/// Implementation of the dialect interface that converts operations belonging
@@ -191,6 +201,8 @@ class LLVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface {
191201
return setTBAAAttr(node, op, moduleImport);
192202
if (kind == llvm::LLVMContext::MD_access_group)
193203
return setAccessGroupAttr(node, op, moduleImport);
204+
if (kind == llvm::LLVMContext::MD_loop)
205+
return setLoopAttr(node, op, moduleImport);
194206

195207
// A handler for a supported metadata kind is missing.
196208
llvm_unreachable("unknown metadata type");

0 commit comments

Comments
 (0)