Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LLVM16: Clang/Wasm unable to export symbols after move to export arg #326

Closed
ericpassmore opened this issue Dec 15, 2024 · 5 comments
Closed
Assignees

Comments

@ericpassmore
Copy link
Contributor

ericpassmore commented Dec 15, 2024

LLVM16 has changed from the CDT-LLVM fork, removing the clang command line option only-export has been removed and replaced by export. Previously only-export would export the last variable/function specified. Now export is additive and allows multiple variables/function to be exposed. In addition, clang introduced the wasm-export-name to enable exports. See Export Name Example in llvm 16.0.6 code base.

You can see the errors when building the unit tests

cd build/tests/unit && cmake --build .

The question is how to fix this. Couple of possibilities on what broke:

  • CDT tools has additional glue code which relies on a side effort of only-export which is no longer being triggered
  • CDT did its own custom wasm export, and LLVM 16 out of the box replaces export with an entirely different implementation.
    • CDT had customized clang to export of apply and *:memory by default and we need to set the right attributed for clang to do the same
    • Clang relies on the export command line flag, and we'll either need to pass that flag everytime or somehow set the key functions to always be exported.

Looking through the code CDT previous defined its own attributes for exported function eosio_wasm_entry and EosioWasmEntryAttr. Here is a quick tour of the code.

clang/lib/Sema/SemaDeclAttr.cpp

/// Applies the given attribute to the Decl without performing any
/// additional semantic checking.
/// S -> Sema , D -> Decl , AL -> AttributeCommonInfo
  case ParsedAttr::AT_EosioWasmEntry:
    handleSimpleAttribute<EosioWasmEntryAttr>(S, D, AL);
    break;

cdt-llvm-extensions/clang/include/clang/Basic/Attr.td

def EosioWasmEntry : InheritableAttr {
   let Spellings = [CXX11<"eosio", "wasm_entry">, GNU<"eosio_wasm_entry">];
   let Subjects = SubjectList<[Function]>;
   let Documentation = [EosioWasmImportDocs];
}

clang/lib/AST/Decl.cpp

bool FunctionDecl::isEosioWasmEntry()const { return hasAttr<EosioWasmEntryAttr>(); }

clang/lib/CodeGen/CodeGenModule.cpp

    if (FD->hasAttr<EosioWasmEntryAttr>())
        isWasmEntry = true;
....
  if (isWasmEntry)
   F->addFnAttr("eosio_wasm_entry", "true");

lvmn/lib/Transforms/EosioApply/EosioApply.cpp

statbool runOnFunction(Function &F) override {
       if (F.hasFnAttribute("eosio_wasm_entry") || F.getName().equals("apply")) {
         auto wasm_ctors = F.getParent()->getOrInsertFunction("__wasm_call_ctors", AttributeList{}, Type::getVoidTy(F.getContext()));
         auto wasm_dtors = F.getParent()->getOrInsertFunction("__cxa_finalize", AttributeList{}, Type::getVoidTy(F.getContext()), Type::getInt32Ty(F.getContext()));
@ericpassmore
Copy link
Contributor Author

ericpassmore commented Dec 15, 2024

Attempt1 - No change

set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/EosioApply.exports) cdt-llvm-extensions/llvm/lib/Transforms/EosioApply/CMakeLists.txt

Attemp2 - No change

Explicitly add wasm attribute in cdt-llvm-extensions/clang/lib/CodeGen/CodeGenModule.cpp

  if (isWasmEntry) {
   F->addFnAttr("eosio_wasm_entry", "true");
   F->addFnAttr("wasm-export-name", F->getName());
  } 

Attempt3 - No Change

set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/EosioApply.exports) cdt-llvm-extensions/llvm/lib/Transforms/EosioApply/CMakeLists.txt and put the string apply in cdt-llvm-extensions/llvm/lib/Transforms/EosioApply/EosioApply.exports

@ericpassmore
Copy link
Contributor Author

ericpassmore commented Dec 15, 2024

Attempt4

Naively use WebAssemblyExportNameAttr in place of EosioWasmEntryAttr

@ericpassmore
Copy link
Contributor Author

ericpassmore commented Dec 15, 2024

Attemp5 - No Change

Decl both attributes in cdt-llvm-extensions/clang/lib/Sema/SemaDeclAttr.cpp

  case ParsedAttr::AT_EosioWasmImport:
    handleWebAssemblyExportNameAttr(S, D, AL);
    handleSimpleAttribute<EosioWasmImportAttr>(S, D, AL);
    break;
  case ParsedAttr::AT_WebAssemblyExportName:
    handleSimpleAttribute<EosioWasmImportAttr>(S, D, AL);
    handleWebAssemblyExportNameAttr(S, D, AL);
    break;

@ericpassmore
Copy link
Contributor Author

ericpassmore commented Dec 15, 2024

Attempt6 - No change

Combine Attempt2 and Attempt6

@ericpassmore ericpassmore changed the title LLVM16: Unable to export function after moving to export LLVM16: No entry point apply function after moving to export Dec 15, 2024
@ericpassmore ericpassmore changed the title LLVM16: No entry point apply function after moving to export LLVM16: No entry point apply after moving to export Dec 15, 2024
@ericpassmore ericpassmore changed the title LLVM16: No entry point apply after moving to export LLVM16: Clang/Wasm unable to export symbols after move to export arg Dec 16, 2024
@ericpassmore
Copy link
Contributor Author

Turns out the apply action isn't getting inserted into contracts. A completely different issue.

@ericpassmore ericpassmore closed this as not planned Won't fix, can't repro, duplicate, stale Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant