This is an archive of the discontinued LLVM Phabricator instance.

[Clang][Attribute] Introduce linkage attribute to specify the exact LLVM linkage type for functions or variables
Needs RevisionPublic

Authored by strimo378 on Aug 17 2023, 7:18 PM.

Details

Summary

Currently, it is not possible to specify all available LLVM linkage types of variables or functions from the C programming language. This patch adds a linkage attribute (__attribute__((linkage("...")))__) that allows to specify the LLVM linkage type https://llvm.org/docs/LangRef.html#linkage-types via an attribute. It is similar to the visible attribute that allows to specify the LLVM visibility styles.

The attribute is dominant and overrides any linkage type specified by other specifiers (e.g. static) or attributes (e.g. __attribute__((weak))).

There was a previous discussion about adding a linkonce_odr_linkage attribute https://reviews.llvm.org/D18095

I use the attribute in my C++-to-C transpiler to generate C code with the same LLVM IR as the original C++ code. Especially, template function use linkonce_odr is relevant for optimization and cannot generated in C right now. Projects like the LLVM C Backend could also profit from it ... https://github.com/JuliaHubOSS/llvm-cbe

Diff Detail

Event Timeline

strimo378 created this revision.Aug 17 2023, 7:18 PM
Herald added a project: Restricted Project. · View Herald Transcript
strimo378 requested review of this revision.Aug 17 2023, 7:18 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 17 2023, 7:19 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
strimo378 edited the summary of this revision. (Show Details)Aug 17 2023, 7:46 PM
strimo378 added reviewers: erichkeane, rjmccall.
rjmccall requested changes to this revision.Aug 18 2023, 12:21 AM

I understand that you're adding this for your project's internal purposes, but it's a permanent language feature that I don't think we want.

This revision now requires changes to proceed.Aug 18 2023, 12:21 AM

I understand that you're adding this for your project's internal purposes, but it's a permanent language feature that I don't think we want.

+1 -- I'm wary of exposing internal LLVM implementation details through a user-facing attribute like this.

I'm wary of exposing internal LLVM implementation details through a user-facing attribute like this.

@aaron.ballman That means you would be more open to an attribute like __attribute__((onedefinitionrule)) because it is part of the C++ standard?

I'm wary of exposing internal LLVM implementation details through a user-facing attribute like this.

@aaron.ballman That means you would be more open to an attribute like __attribute__((onedefinitionrule)) because it is part of the C++ standard?

It depends entirely on what the attribute would do, how it is used in-tree, how users would use it, etc.