[llvm] [KernelInfo] Implement new LLVM IR pass for GPU code analysis (PR #102944)

Joel E. Denny via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 16 13:26:57 PDT 2024


================
@@ -298,90 +284,30 @@ static void remarkProperty(OptimizationRemarkEmitter &ORE, const Function &F,
   remarkProperty(ORE, F, Name, Value.value());
 }
 
-static std::vector<std::optional<int64_t>>
-parseFnAttrAsIntegerFields(Function &F, StringRef Name, unsigned NumFields) {
-  std::vector<std::optional<int64_t>> Result(NumFields);
-  Attribute A = F.getFnAttribute(Name);
-  if (!A.isStringAttribute())
-    return Result;
-  StringRef Rest = A.getValueAsString();
-  for (unsigned I = 0; I < NumFields; ++I) {
-    StringRef Field;
-    std::tie(Field, Rest) = Rest.split(',');
-    if (Field.empty())
-      break;
-    int64_t Val;
-    if (Field.getAsInteger(0, Val)) {
-      F.getContext().emitError("cannot parse integer in attribute '" + Name +
-                               "': " + Field);
-      break;
-    }
-    Result[I] = Val;
-  }
-  if (!Rest.empty())
-    F.getContext().emitError("too many fields in attribute " + Name);
-  return Result;
-}
-
 static std::optional<int64_t> parseFnAttrAsInteger(Function &F,
                                                    StringRef Name) {
-  return parseFnAttrAsIntegerFields(F, Name, 1)[0];
-}
-
-// TODO: This nearly duplicates the same function in OMPIRBuilder.cpp.  Can we
-// share?
-static MDNode *getNVPTXMDNode(Function &F, StringRef Name) {
-  Module &M = *F.getParent();
-  NamedMDNode *MD = M.getNamedMetadata("nvvm.annotations");
-  if (!MD)
-    return nullptr;
-  for (auto *Op : MD->operands()) {
-    if (Op->getNumOperands() != 3)
-      continue;
-    auto *KernelOp = dyn_cast<ConstantAsMetadata>(Op->getOperand(0));
-    if (!KernelOp || KernelOp->getValue() != &F)
-      continue;
-    auto *Prop = dyn_cast<MDString>(Op->getOperand(1));
-    if (!Prop || Prop->getString() != Name)
-      continue;
-    return Op;
-  }
-  return nullptr;
-}
-
-static std::optional<int64_t> parseNVPTXMDNodeAsInteger(Function &F,
-                                                        StringRef Name) {
-  std::optional<int64_t> Result;
-  if (MDNode *ExistingOp = getNVPTXMDNode(F, Name)) {
-    auto *Op = cast<ConstantAsMetadata>(ExistingOp->getOperand(2));
-    Result = cast<ConstantInt>(Op->getValue())->getZExtValue();
+  Attribute A = F.getFnAttribute(Name);
+  if (!A.isStringAttribute())
+    return std::nullopt;
+  StringRef Field = A.getValueAsString();
+  int64_t Val;
+  if (Field.getAsInteger(0, Val)) {
+    F.getContext().emitError("cannot parse integer in attribute '" + Name +
+                             "': " + Field);
+    return std::nullopt;
----------------
jdenny-ornl wrote:

I rewrote it to call getFnAttributeAsParsedInteger.

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


More information about the llvm-commits mailing list