[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(
+    Location loc, Type type, ArrayRef<llvm::Value *> llvmOperands,
+    StringRef opName) {
+  // Split metadata values from regular ones.
+  SmallVector<llvm::Value *> values;
+  SmallVector<llvm::Metadata *> metadata;
+  splitMetadataAndValues(llvmOperands, values, metadata);
+
+  // Expect 1 or 2 metadata values.
+  assert((metadata.size() == 1 || metadata.size() == 2) &&
+         "Unexpected number of arguments");
+
+  SmallVector<Value> mlirOperands;
+  SmallVector<NamedAttribute> mlirAttrs;
+  if (failed(
+          convertIntrinsicArguments(values, {}, {}, mlirOperands, mlirAttrs))) {
+    return {};
+  }
+
+  // Create operation as usual.
+  StringAttr opNameAttr = builder.getStringAttr(opName);
+  Operation *op =
+      builder.create(loc, opNameAttr, mlirOperands, type, mlirAttrs);
+
+  // Set exception behavior attribute.
+  auto exceptionBehaviorOp = cast<ExceptionBehaviorOpInterface>(op);
+  ExceptionBehaviorAttr attr =
+      metadataToExceptionBehavior(builder, metadata.back());
+  if (!attr)
+    return {};
+  op->setAttr(exceptionBehaviorOp.getExceptionBehaviorAttrName(), attr);
----------------
gysit wrote:

Here the set function of the interface could be used! Similary below for the rounding mode.

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


More information about the Mlir-commits mailing list