Skip to content

Commit

Permalink
[CIR][CIRGen] Ensure default visibility for local linkage functions
Browse files Browse the repository at this point in the history
LLVM's verifier enforces this, which was previously causing us to fail
verification. This is a bit of a band-aid; the overall linkage and
visibility setting flow needs some work to match the original.
  • Loading branch information
smeenai committed Oct 17, 2024
1 parent 199228d commit b63bfa4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
11 changes: 9 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2608,8 +2608,15 @@ void CIRGenModule::setFunctionAttributes(GlobalDecl globalDecl,

// TODO(cir): Complete the remaining part of the function.
assert(!MissingFeatures::setFunctionAttributes());
auto decl = globalDecl.getDecl();
func.setGlobalVisibilityAttr(getGlobalVisibilityAttrFromDecl(decl));

// TODO(cir): This needs a lot of work to better match CodeGen. That
// ultimately ends up in setGlobalVisibility, which already has the linkage of
// the LLVM GV (corresponding to our FuncOp) computed, so it doesn't have to
// recompute it here. This is a minimal fix for now.
if (!isLocalLinkage(getFunctionLinkage(globalDecl))) {
auto decl = globalDecl.getDecl();
func.setGlobalVisibilityAttr(getGlobalVisibilityAttrFromDecl(decl));
}
}

/// If the specified mangled name is not in the module,
Expand Down
15 changes: 15 additions & 0 deletions clang/test/CIR/CodeGen/visibility-attribute.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,24 @@ void __attribute__((__visibility__("protected"))) foo_protected();
// CIR: cir.func no_proto private protected @foo_protected(...)
// LLVM: declare {{.*}} protected void @foo_protected(...)

static void static_foo_default() {}
// CIR: cir.func no_proto internal private @static_foo_default()
// LLVM: define internal void @static_foo_default()

static void __attribute__((__visibility__("hidden"))) static_foo_hidden() {}
// CIR: cir.func no_proto internal private @static_foo_hidden()
// LLVM: define internal void @static_foo_hidden()

static void __attribute__((__visibility__("protected"))) static_foo_protected() {}
// CIR: cir.func no_proto internal private @static_foo_protected()
// LLVM: define internal void @static_foo_protected()

void call_foo()
{
foo_default();
foo_hidden();
foo_protected();
static_foo_default();
static_foo_hidden();
static_foo_protected();
}

0 comments on commit b63bfa4

Please sign in to comment.