[clang] Fix CIR_GlobalOp global_visibility assembly format and add tests (PR #190195)

Satyam Kulkarni via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 2 08:29:25 PDT 2026


https://github.com/sat-06 created https://github.com/llvm/llvm-project/pull/190195

Closes #189666 

This PR addresses two issues with cir.global operations:
1. Incorrect printing and parsing of global_visibility:
Using __attribute__((visibility("hidden"))) or __attribute__((visibility("private"))) on global variables previously produced invalid CIR that could not be parsed back.

Previous incorrect CIR:
```mlir
cir.global external dso_local @normal_var = #cir.int<10> : !s32i {alignment = 4 : i64} loc(#loc21)
cir.globalhidden external dso_local @hidden_var = #cir.int<10> : !s32i {alignment = 4 : i64} loc(#loc22)
cir.global "private"hidden internal dso_local @hidden_static_var = #cir.int<10> : !s32i {alignment = 4 : i64} loc(#loc24)
```
Keywords were sticking to previous words, causing parsing failures.
The fix uses a custom VisibilityAttr printer in assemblyFormat:
```mlir
let assemblyFormat = [{
    ($sym_visibility^)?
    (custom<VisibilityAttr>($global_visibility)^)?
    (`constant` $constant^)?
    $linkage
    (`comdat` $comdat^)?
    ($tls_model^)?
    (`dso_local` $dso_local^)?
    (`static_local_guard` `` $static_local_guard^)?
    (` ` custom<GlobalAddressSpaceValue>($addr_space)^ )?
    $sym_name
    custom<GlobalOpTypeAndInitialValue>($sym_type, $initial_value,
                                        $ctorRegion, $dtorRegion)
    attr-dict
}];
```
2. Fixed extra space issue in assembly printing:
Previously, the global_visibility keyword would stick to the previous word if printed, e.g., cir.globalhidden.
Now, printing and parsing of both global values and functions is consistent with the expected CIR format.

Testing:
Added unit tests for global values and functions to verify correct printing and parsing of visibility attributes.
Ensures assembly format now matches expected output for all CIR global declarations.

>From 3b88b923bc8e3e3aa9db10f8d4b855839287817b Mon Sep 17 00:00:00 2001
From: Satyam Kulkarni <kulkarnisatyam666 at gmail.com>
Date: Thu, 2 Apr 2026 20:47:28 +0530
Subject: [PATCH] Fix CIR_GlobalOp global_visibility assembly format and add
 tests

---
 clang/include/clang/CIR/Dialect/IR/CIROps.td | 29 ++++++++++----------
 clang/test/CIR/CodeGen/global-visibility.c   | 18 ++++++++++++
 2 files changed, 32 insertions(+), 15 deletions(-)
 create mode 100644 clang/test/CIR/CodeGen/global-visibility.c

diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 56c7b48afbaf5..df70e551506f3 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -2849,21 +2849,20 @@ def CIR_GlobalOp : CIR_Op<"global", [
   let regions = (region MaxSizedRegion<1>:$ctorRegion,
                         MaxSizedRegion<1>:$dtorRegion);
 
-  let assemblyFormat = [{
-    ($sym_visibility^)?
-    (`` $global_visibility^)?
-    (`constant` $constant^)?
-    $linkage
-    (`comdat` $comdat^)?
-    ($tls_model^)?
-    (`dso_local` $dso_local^)?
-    (`static_local_guard` `` $static_local_guard^)?
-    (` ` custom<GlobalAddressSpaceValue>($addr_space)^ )?
-    $sym_name
-    custom<GlobalOpTypeAndInitialValue>($sym_type, $initial_value,
-                                        $ctorRegion, $dtorRegion)
-    attr-dict
-  }];
+ let assemblyFormat = [{
+  (` ` $global_visibility^)?
+  (`constant` $constant^)?
+  $linkage
+  (`comdat` $comdat^)?
+  ($tls_model^)?
+  (`dso_local` $dso_local^)?
+  (`static_local_guard` `` $static_local_guard^)?
+  (` ` custom<GlobalAddressSpaceValue>($addr_space)^ )?
+  $sym_name
+  custom<GlobalOpTypeAndInitialValue>($sym_type, $initial_value,
+                                      $ctorRegion, $dtorRegion)
+  attr-dict
+}];
 
   let extraClassDeclaration = [{
     bool isDeclaration() {
diff --git a/clang/test/CIR/CodeGen/global-visibility.c b/clang/test/CIR/CodeGen/global-visibility.c
new file mode 100644
index 0000000000000..f90a742c57f40
--- /dev/null
+++ b/clang/test/CIR/CodeGen/global-visibility.c
@@ -0,0 +1,18 @@
+// RUN: cir-clang %s -O0 -S -emit-cir | FileCheck %s
+
+int normal_var = 10;
+
+__attribute__((visibility("hidden")))
+int hidden_var = 10;
+
+__attribute__((visibility("hidden")))
+static int hidden_static_var = 10;
+
+// CHECK-LABEL: cir.global external dso_local @normal_var
+// CHECK: #cir.int<10> : !s32i
+
+// CHECK-LABEL: cir.global hidden external dso_local @hidden_var
+// CHECK: #cir.int<10> : !s32i
+
+// CHECK-LABEL: cir.global hidden internal dso_local @hidden_static_var
+// CHECK: #cir.int<10> : !s32i
\ No newline at end of file



More information about the cfe-commits mailing list