[Mlir-commits] [mlir] bfea837 - [mlir][llvm] Reintroduce string based attribute setting.

Tobias Gysi llvmlistbot at llvm.org
Mon Feb 13 23:12:35 PST 2023


Author: Tobias Gysi
Date: 2023-02-14T08:09:58+01:00
New Revision: bfea837676038ef3e312563b15811b524cbfb6aa

URL: https://github.com/llvm/llvm-project/commit/bfea837676038ef3e312563b15811b524cbfb6aa
DIFF: https://github.com/llvm/llvm-project/commit/bfea837676038ef3e312563b15811b524cbfb6aa.diff

LOG: [mlir][llvm] Reintroduce string based attribute setting.

Reintroduce string based attribute setting in the
translation from LLVM dialect to LLVM IR. The TypeSwitch
based implementation introduced in
https://reviews.llvm.org/D143654  does not work for
intrinsics that set the requiresAccessGroup or
requiresAliasScope flag.

Reviewed By: hgreving

Differential Revision: https://reviews.llvm.org/D143936

Added: 
    

Modified: 
    mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index 3834bf02dcbcf..c928d78fcc558 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -27,7 +27,6 @@
 #include "mlir/Support/LLVM.h"
 #include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
 #include "mlir/Target/LLVMIR/TypeToLLVM.h"
-#include "llvm/ADT/TypeSwitch.h"
 
 #include "llvm/ADT/PostOrderIterator.h"
 #include "llvm/ADT/SetVector.h"
@@ -1010,13 +1009,13 @@ LogicalResult ModuleTranslation::createAccessGroupMetadata() {
 
 void ModuleTranslation::setAccessGroupsMetadata(Operation *op,
                                                 llvm::Instruction *inst) {
-  auto populateGroupsMetadata = [&](std::optional<ArrayAttr> groupRefs) {
-    if (!groupRefs || groupRefs->empty())
+  auto populateGroupsMetadata = [&](ArrayAttr groupRefs) {
+    if (!groupRefs || groupRefs.empty())
       return;
 
     llvm::Module *module = inst->getModule();
     SmallVector<llvm::Metadata *> groupMDs;
-    for (SymbolRefAttr groupRef : groupRefs->getAsRange<SymbolRefAttr>())
+    for (SymbolRefAttr groupRef : groupRefs.getAsRange<SymbolRefAttr>())
       groupMDs.push_back(getAccessGroup(op, groupRef));
 
     llvm::MDNode *node = nullptr;
@@ -1028,10 +1027,9 @@ void ModuleTranslation::setAccessGroupsMetadata(Operation *op,
     inst->setMetadata(llvm::LLVMContext::MD_access_group, node);
   };
 
-  llvm::TypeSwitch<Operation *>(op)
-      .Case<LoadOp, StoreOp>(
-          [&](auto memOp) { populateGroupsMetadata(memOp.getAccessGroups()); })
-      .Default([](auto) { llvm_unreachable("expected LoadOp or StoreOp"); });
+  auto groupRefs =
+      op->getAttrOfType<ArrayAttr>(LLVMDialect::getAccessGroupsAttrName());
+  populateGroupsMetadata(groupRefs);
 }
 
 LogicalResult ModuleTranslation::createAliasScopeMetadata() {
@@ -1085,26 +1083,24 @@ ModuleTranslation::getAliasScope(Operation *op,
 
 void ModuleTranslation::setAliasScopeMetadata(Operation *op,
                                               llvm::Instruction *inst) {
-  auto populateScopeMetadata = [&](std::optional<ArrayAttr> scopeRefs,
-                                   unsigned kind) {
-    if (!scopeRefs || scopeRefs->empty())
+  auto populateScopeMetadata = [&](ArrayAttr scopeRefs, unsigned kind) {
+    if (!scopeRefs || scopeRefs.empty())
       return;
     llvm::Module *module = inst->getModule();
     SmallVector<llvm::Metadata *> scopeMDs;
-    for (SymbolRefAttr scopeRef : scopeRefs->getAsRange<SymbolRefAttr>())
+    for (SymbolRefAttr scopeRef : scopeRefs.getAsRange<SymbolRefAttr>())
       scopeMDs.push_back(getAliasScope(op, scopeRef));
     llvm::MDNode *node = llvm::MDNode::get(module->getContext(), scopeMDs);
     inst->setMetadata(kind, node);
   };
 
-  llvm::TypeSwitch<Operation *>(op)
-      .Case<LoadOp, StoreOp>([&](auto memOp) {
-        populateScopeMetadata(memOp.getAliasScopes(),
-                              llvm::LLVMContext::MD_alias_scope);
-        populateScopeMetadata(memOp.getNoaliasScopes(),
-                              llvm::LLVMContext::MD_noalias);
-      })
-      .Default([](auto) { llvm_unreachable("expected LoadOp or StoreOp"); });
+  auto aliasScopeRefs =
+      op->getAttrOfType<ArrayAttr>(LLVMDialect::getAliasScopesAttrName());
+  populateScopeMetadata(aliasScopeRefs, llvm::LLVMContext::MD_alias_scope);
+
+  auto noaliasScopeRefs =
+      op->getAttrOfType<ArrayAttr>(LLVMDialect::getNoAliasScopesAttrName());
+  populateScopeMetadata(noaliasScopeRefs, llvm::LLVMContext::MD_noalias);
 }
 
 llvm::MDNode *ModuleTranslation::getTBAANode(Operation *op,


        


More information about the Mlir-commits mailing list