[Mlir-commits] [mlir] [MLIR][LLVM] Attach kernel metadata representation to `llvm.func` (PR #101314)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Aug 1 07:55:47 PDT 2024


================
@@ -226,6 +239,128 @@ static LogicalResult setNoaliasScopesAttr(const llvm::MDNode *node,
   return success();
 }
 
+/// Extracts an integer from the provided metadata `md` if possible. Returns
+/// nullopt otherwise.
+static std::optional<int32_t> parseIntegerMD(llvm::Metadata *md) {
+  auto *constant = dyn_cast_if_present<llvm::ConstantAsMetadata>(md);
+  if (!constant)
+    return {};
+
+  auto *intConstant = dyn_cast<llvm::ConstantInt>(constant->getValue());
+  if (!intConstant)
+    return {};
+
+  return intConstant->getValue().getSExtValue();
+}
+
+/// Converts the provided metadata node `node` to an LLVM dialect
+/// VecTypeHintAttr if possible.
+static VecTypeHintAttr convertVecTypeHint(Builder builder, llvm::MDNode *node,
+                                          ModuleImport &moduleImport) {
+  if (!node || node->getNumOperands() != 2)
+    return {};
+
+  auto *hintMD = dyn_cast<llvm::ValueAsMetadata>(node->getOperand(0).get());
+  if (!hintMD)
+    return {};
+  TypeAttr hint = TypeAttr::get(moduleImport.convertType(hintMD->getType()));
+
+  std::optional<int32_t> optIsSigned =
+      parseIntegerMD(node->getOperand(1).get());
+  if (!optIsSigned)
+    return {};
----------------
FMarno wrote:

when `optIsSigned` is nullopt, that seems like some kind of invalid state. Maybe there should be an error or assert here?

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


More information about the Mlir-commits mailing list