1 //===- LoopAnnotationTranslation.h ------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the translation between an MLIR loop annotations and 10 // the corresponding LLVMIR metadata representation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONTRANSLATION_H_ 15 #define MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONTRANSLATION_H_ 16 17 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 18 #include "mlir/Target/LLVMIR/ModuleTranslation.h" 19 20 namespace mlir { 21 namespace LLVM { 22 namespace detail { 23 24 /// A helper class that converts LoopAnnotationAttrs and AccessGroupAttrs into 25 /// corresponding llvm::MDNodes. 26 class LoopAnnotationTranslation { 27 public: LoopAnnotationTranslation(ModuleTranslation & moduleTranslation,llvm::Module & llvmModule)28 LoopAnnotationTranslation(ModuleTranslation &moduleTranslation, 29 llvm::Module &llvmModule) 30 : moduleTranslation(moduleTranslation), llvmModule(llvmModule) {} 31 32 llvm::MDNode *translateLoopAnnotation(LoopAnnotationAttr attr, Operation *op); 33 34 /// Returns the LLVM metadata corresponding to an mlir LLVM dialect access 35 /// group attribute. 36 llvm::MDNode *getAccessGroup(AccessGroupAttr accessGroupAttr); 37 38 /// Returns the LLVM metadata corresponding to the access group attribute 39 /// referenced by the AccessGroupOpInterface or null if there are none. 40 llvm::MDNode *getAccessGroups(AccessGroupOpInterface op); 41 42 /// The ModuleTranslation owning this instance. 43 ModuleTranslation &moduleTranslation; 44 45 private: 46 /// Returns the LLVM metadata corresponding to a llvm loop metadata attribute. lookupLoopMetadata(Attribute options)47 llvm::MDNode *lookupLoopMetadata(Attribute options) const { 48 return loopMetadataMapping.lookup(options); 49 } 50 mapLoopMetadata(Attribute options,llvm::MDNode * metadata)51 void mapLoopMetadata(Attribute options, llvm::MDNode *metadata) { 52 auto result = loopMetadataMapping.try_emplace(options, metadata); 53 (void)result; 54 assert(result.second && 55 "attempting to map loop options that was already mapped"); 56 } 57 58 /// Mapping from an attribute describing loop metadata to its LLVM metadata. 59 /// The metadata is attached to Latch block branches with this attribute. 60 DenseMap<Attribute, llvm::MDNode *> loopMetadataMapping; 61 62 /// Mapping from an access group attribute to its LLVM metadata. 63 /// This map is populated on module entry and is used to annotate loops (as 64 /// identified via their branches) and contained memory accesses. 65 DenseMap<AccessGroupAttr, llvm::MDNode *> accessGroupMetadataMapping; 66 67 llvm::Module &llvmModule; 68 }; 69 70 } // namespace detail 71 } // namespace LLVM 72 } // namespace mlir 73 74 #endif // MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONTRANSLATION_H_ 75