Skip to content

IRToASTVisitor: preserve external LLVM global declarations as C extern #352

Open

Description

@humdrum00001010

Rellic emits @counter = external global i32 as unsigned int counter;.
That C declaration creates a tentative definition. Linking the emitted C
with the original data provider therefore fails with a duplicate symbol.

target triple = "i686-pc-windows-msvc"
@counter = external global i32
define i32 @read_counter() {
  %value = load i32, ptr @counter
  ret i32 %value
}

Expected output is extern unsigned int counter;. The narrow fix passes
SC_Extern to CreateVarDecl when the LLVM global is a declaration with
ordinary external linkage. Initialized definitions keep their existing
storage class and initializer.

The independently synthetic fixture in tests/external-global-declarations/
contains five complete functions, three external globals and one owned global
initialized to 11. A separate provider defines the external data. With
Clang/LLVM 17.0.6, Windows i686 and -fno-common, the original LLVM module
links with the provider; baseline emitted C fails with three duplicate
definitions. Corrected emitted C links, retains three undefined consumer
symbols and preserves the owned initializer. Linking without the provider
fails. Reccmp compares all five function bodies; all are byte-identical.
No runtime execution or argument sampling is performed.

Standalone whole modules: https://github.com/humdrum00001010/rellic/tree/dd9d094f7bad8c23c87833e7dd8171c344ee83d0/tests/external-global-declarations

Generic fix: https://github.com/humdrum00001010/rellic/commit/dd9d094f7bad8c23c87833e7dd8171c344ee83d0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions