[clang] [CIR] Implement setGlobalVisibility (PR #195257)
Zaky Hermawan via cfe-commits
cfe-commits at lists.llvm.org
Fri May 1 04:32:54 PDT 2026
https://github.com/ZakyHermawan created https://github.com/llvm/llvm-project/pull/195257
Matching OGCG
>From 9e693e6e5a3ccde79dc4e7c3ce4c186b6b1c47e2 Mon Sep 17 00:00:00 2001
From: ZakyHermawan <zaky.hermawan9615 at gmail.com>
Date: Fri, 1 May 2026 18:26:16 +0700
Subject: [PATCH] [CIR] Implement setGlobalVisibility
Signed-off-by: ZakyHermawan <zaky.hermawan9615 at gmail.com>
---
.../clang/CIR/Interfaces/CIROpInterfaces.td | 12 +++
clang/lib/CIR/CodeGen/CIRGenModule.cpp | 74 ++++++++++++++++++-
clang/lib/CIR/CodeGen/CIRGenModule.h | 15 +++-
3 files changed, 97 insertions(+), 4 deletions(-)
diff --git a/clang/include/clang/CIR/Interfaces/CIROpInterfaces.td b/clang/include/clang/CIR/Interfaces/CIROpInterfaces.td
index 898e28964eef0..181397c1809aa 100644
--- a/clang/include/clang/CIR/Interfaces/CIROpInterfaces.td
+++ b/clang/include/clang/CIR/Interfaces/CIROpInterfaces.td
@@ -145,6 +145,18 @@ let cppNamespace = "::cir" in {
}]
>,
InterfaceMethod<"",
+ "void", "setGlobalVisibility", (ins "cir::VisibilityKind":$val), [{}],
+ /*defaultImplementation=*/[{
+ $_op.setGlobalVisibility(val);
+ }]
+ >,
+ InterfaceMethod<"",
+ "void", "setLinkage", (ins "cir::GlobalLinkageKind":$val), [{}],
+ /*defaultImplementation=*/[{
+ $_op.setLinkage(val);
+ }]
+ >,
+ InterfaceMethod<"",
"bool", "isDSOLocal", (ins), [{}],
/*defaultImplementation=*/[{
return $_op.getDsoLocal();
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index af8fd52bef017..2239a07e3ef4b 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -2702,9 +2702,77 @@ static bool shouldAssumeDSOLocal(const CIRGenModule &cgm,
return false;
}
-void CIRGenModule::setGlobalVisibility(mlir::Operation *gv,
+static void setGlobalVisibilityHelper(const CIRGenModule &cgm,
+ cir::CIRGlobalValueInterface gv,
+ cir::VisibilityKind visibility) {
+ gv.setGlobalVisibility(cir::VisibilityKind::Default);
+ // Also update MLIR symbol visibility to match linkage
+ if (auto globalOp = dyn_cast<cir::GlobalOp>(gv.getOperation()))
+ mlir::SymbolTable::setSymbolVisibility(globalOp,
+ cgm.getMLIRVisibility(globalOp));
+ else if (auto funcOp = dyn_cast<cir::FuncOp>(gv.getOperation()))
+ mlir::SymbolTable::setSymbolVisibility(
+ funcOp, cgm.getMLIRVisibilityFromCIRLinkage(funcOp.getLinkage()));
+}
+
+void CIRGenModule::setGlobalVisibility(cir::CIRGlobalValueInterface gv,
const NamedDecl *d) const {
- assert(!cir::MissingFeatures::opGlobalVisibility());
+ // Internal definitions always have default visibility.
+ if (gv.hasLocalLinkage()) {
+ setGlobalVisibilityHelper(*this, gv, cir::VisibilityKind::Default);
+ return;
+ }
+ if (!d)
+ return;
+
+ // Set visibility for definitions, and for declarations if requested globally
+ // or set explicitly.
+ LinkageInfo lv = d->getLinkageAndVisibility();
+
+ // OpenMP declare target variables must be visible to the host so they can
+ // be registered. We require protected visibility unless the variable has
+ // the DT_nohost modifier and does not need to be registered.
+ if (getASTContext().getLangOpts().OpenMP &&
+ getASTContext().getLangOpts().OpenMPIsTargetDevice && isa<VarDecl>(d) &&
+ d->hasAttr<OMPDeclareTargetDeclAttr>() &&
+ d->getAttr<OMPDeclareTargetDeclAttr>()->getDevType() !=
+ OMPDeclareTargetDeclAttr::DT_NoHost &&
+ lv.getVisibility() == HiddenVisibility) {
+ gv.setGlobalVisibility(cir::VisibilityKind::Protected);
+ return;
+ }
+
+ // CUDA/HIP device kernels and global variables must be visible to the host
+ // so they can be registered / initialized. We require protected visibility
+ // unless the user explicitly requested hidden via an attribute.
+ if (getASTContext().getLangOpts().CUDAIsDevice &&
+ lv.getVisibility() == HiddenVisibility && !lv.isVisibilityExplicit() &&
+ !d->hasAttr<OMPDeclareTargetDeclAttr>()) {
+ bool needsProtected = false;
+ if (isa<FunctionDecl>(d)) {
+ needsProtected =
+ d->hasAttr<CUDAGlobalAttr>() || d->hasAttr<DeviceKernelAttr>();
+ } else if (const auto *vd = dyn_cast<VarDecl>(d))
+ needsProtected = vd->hasAttr<CUDADeviceAttr>() ||
+ vd->hasAttr<CUDAConstantAttr>() ||
+ vd->getType()->isCUDADeviceBuiltinSurfaceType() ||
+ vd->getType()->isCUDADeviceBuiltinTextureType();
+ if (needsProtected) {
+ gv.setGlobalVisibility(cir::VisibilityKind::Protected);
+ return;
+ }
+ }
+
+ if (getASTContext().getLangOpts().HLSL && !d->isInExportDeclContext()) {
+ gv.setGlobalVisibility(cir::VisibilityKind::Hidden);
+ return;
+ }
+
+ assert(!cir::MissingFeatures::opGlobalDLLImportExport());
+
+ if (lv.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls ||
+ !gv.isDeclarationForLinker())
+ gv.setGlobalVisibility(getCIRVisibilityKind(lv.getVisibility()));
}
void CIRGenModule::setDSOLocal(cir::CIRGlobalValueInterface gv) const {
@@ -2724,7 +2792,7 @@ void CIRGenModule::setGVProperties(mlir::Operation *op,
void CIRGenModule::setGVPropertiesAux(mlir::Operation *op,
const NamedDecl *d) const {
- setGlobalVisibility(op, d);
+ setGlobalVisibility(cast<cir::CIRGlobalValueInterface>(op), d);
setDSOLocal(op);
assert(!cir::MissingFeatures::opGlobalPartition());
}
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h
index 2869411015bc5..f388e84ad55ed 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.h
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.h
@@ -431,6 +431,19 @@ class CIRGenModule : public CIRGenTypeCache {
llvm_unreachable("unknown visibility!");
}
+ static cir::VisibilityKind getCIRVisibilityKind(Visibility v) {
+ switch (v) {
+ case DefaultVisibility:
+ return cir::VisibilityKind::Default;
+ case HiddenVisibility:
+ return cir::VisibilityKind::Hidden;
+ case ProtectedVisibility:
+ return cir::VisibilityKind::Protected;
+ }
+
+ llvm_unreachable("unknown visibility!");
+ }
+
llvm::DenseMap<mlir::Attribute, cir::GlobalOp> constantStringMap;
llvm::DenseMap<const UnnamedGlobalConstantDecl *, cir::GlobalOp>
unnamedGlobalConstantDeclMap;
@@ -599,7 +612,7 @@ class CIRGenModule : public CIRGenTypeCache {
mlir::Type convertType(clang::QualType type);
/// Set the visibility for the given global.
- void setGlobalVisibility(mlir::Operation *op, const NamedDecl *d) const;
+ void setGlobalVisibility(cir::CIRGlobalValueInterface gv, const NamedDecl *d) const;
void setDSOLocal(mlir::Operation *op) const;
void setDSOLocal(cir::CIRGlobalValueInterface gv) const;
More information about the cfe-commits
mailing list