[clang] [CIR] Annotate AMDGPU printf ops with constant strings before lowering (PR #242111)

Steffen Larsen via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 13 00:17:07 PST 2026


================
@@ -76,45 +149,46 @@ bool getConstantStringInfo(mlir::Value ptr, mlir::ModuleOp mod,
   auto getGlobal = ptr.getDefiningOp<cir::GetGlobalOp>();
   if (!getGlobal)
     return false;
-
-  // The global may or may not have been converted already.
-  std::string data;
   mlir::Operation *symbol =
       symbolTables.lookupSymbolIn(mod, getGlobal.getNameAttr());
-  if (auto global = mlir::dyn_cast_if_present<cir::GlobalOp>(symbol)) {
-    std::optional<mlir::Attribute> init = global.getInitialValue();
-    auto arrayTy = mlir::dyn_cast<cir::ArrayType>(global.getSymType());
-    if (!global.getConstant() || !init || !arrayTy ||
-        !isByteType(arrayTy.getElementType()))
-      return false;
-    if (auto constArr = mlir::dyn_cast<cir::ConstArrayAttr>(*init)) {
-      auto strAttr = mlir::dyn_cast<mlir::StringAttr>(constArr.getElts());
-      if (!strAttr)
-        return false;
-      data = strAttr.getValue().str();
-    } else if (!mlir::isa<cir::ZeroAttr>(*init)) {
-      return false;
-    }
-    // Add back the trailing NULs the string attribute leaves out.
-    data.resize(arrayTy.getSize(), '\0');
-  } else if (auto global =
-                 mlir::dyn_cast_if_present<mlir::LLVM::GlobalOp>(symbol)) {
-    if (!global.getConstant())
-      return false;
-    mlir::Attribute value = global.getValueOrNull();
-    if (auto strAttr = mlir::dyn_cast_if_present<mlir::StringAttr>(value)) {
+  auto global = mlir::dyn_cast_if_present<cir::GlobalOp>(symbol);
+  if (!global)
+    return false;
+
+  std::string data;
+  std::optional<mlir::Attribute> init = global.getInitialValue();
+  auto arrayTy = mlir::dyn_cast<cir::ArrayType>(global.getSymType());
+  if (!global.getConstant() || !init || !arrayTy ||
+      !isByteType(arrayTy.getElementType()))
+    return false;
+  if (auto constArr = mlir::dyn_cast<cir::ConstArrayAttr>(*init)) {
+    if (auto strAttr = mlir::dyn_cast<mlir::StringAttr>(constArr.getElts())) {
       data = strAttr.getValue().str();
-    } else if (auto dense =
-                   mlir::dyn_cast_if_present<mlir::DenseIntElementsAttr>(value);
-               dense && dense.getElementType().isInteger(8)) {
-      for (const llvm::APInt &c : dense.getValues<llvm::APInt>())
-        data.push_back(static_cast<char>(c.getZExtValue()));
+    } else if (auto arrayAttr =
+                   mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts())) {
+      for (mlir::Attribute elt : arrayAttr) {
----------------
steffenlarsen wrote:

```suggestion
      data.reserve(max(arrayAttr.size(), arrayTy.getSize()));
      for (mlir::Attribute elt : arrayAttr) {
```
I added the max as it should also make the later resizing cheap, though I wonder if `arrayTy` will always dominate `arrayAttr`. I would hope so.

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


More information about the cfe-commits mailing list