[Mlir-commits] [mlir] [MLIR][LLVM] Add `llvm.experimental.constrained.fptrunc` operation (PR #86260)

Tobias Gysi llvmlistbot at llvm.org
Fri Mar 22 05:41:47 PDT 2024


================
@@ -1258,6 +1258,92 @@ LogicalResult ModuleImport::convertIntrinsicArguments(
   return success();
 }
 
+static RoundingModeAttr metadataToRoundingMode(Builder &builder,
+                                               llvm::Metadata *metadata) {
+  auto *mdstr = dyn_cast<llvm::MDString>(metadata);
+  if (!mdstr)
+    return {};
+  std::optional<llvm::RoundingMode> optLLVM =
+      llvm::convertStrToRoundingMode(mdstr->getString());
+  if (!optLLVM)
+    return {};
+  return builder.getAttr<RoundingModeAttr>(
+      convertRoundingModeFromLLVM(*optLLVM));
+}
+
+static ExceptionBehaviorAttr
+metadataToExceptionBehavior(Builder &builder, llvm::Metadata *metadata) {
+  auto *mdstr = dyn_cast<llvm::MDString>(metadata);
+  if (!mdstr)
+    return {};
+  std::optional<llvm::fp::ExceptionBehavior> optLLVM =
+      llvm::convertStrToExceptionBehavior(mdstr->getString());
+  if (!optLLVM)
+    return {};
+  return builder.getAttr<ExceptionBehaviorAttr>(
+      convertExceptionBehaviorFromLLVM(*optLLVM));
+}
+
+static void
+splitMetadataAndValues(ArrayRef<llvm::Value *> inputs,
+                       SmallVectorImpl<llvm::Value *> &values,
+                       SmallVectorImpl<llvm::Metadata *> &metadata) {
+  for (llvm::Value *in : inputs) {
+    if (auto *mdval = dyn_cast<llvm::MetadataAsValue>(in)) {
+      metadata.push_back(mdval->getMetadata());
+    } else {
+      values.push_back(in);
+    }
+  }
+}
+
+Value ModuleImport::translateConstrainedIntrinsic(
----------------
gysit wrote:

The preferred way for importing attributes are the matcher functions we introduced for other attributes. We then added a tablegen shortcut so that the attributes can be parsed directly in tablegen.

A good example is `matchLabelAttr`: 
```
DILabelAttr ModuleImport::matchLabelAttr(llvm::Value *value) {
  auto *nodeAsVal = cast<llvm::MetadataAsValue>(value);
  auto *node = cast<llvm::DILabel>(nodeAsVal->getMetadata());
  return debugImporter->translate(node);
}
```
It is then add to LLVMIRConversionGen.cpp:
```
    } else if (name == "_label_attr") {
      bs << "moduleImport.matchLabelAttr";
```

Which then can be used inside the mlir builder:
```
  let mlirBuilder = [{
    DILabelAttr labelAttr = $_label_attr($label);
    // Drop the intrinsic if the label translation fails due to cylic metadata.
    if (!labelAttr)
      return success();
    $_op = $_builder.create<$_qualCppClassName>($_location, labelAttr);
  }];
```
That way the main import logic can hopefully be moved into tablegen?

Would something similar to matchLabelAttr work for you as well?

https://github.com/llvm/llvm-project/pull/86260


More information about the Mlir-commits mailing list