Skip to content

Commit

Permalink
Add wholearchive thin lib repro
Browse files Browse the repository at this point in the history
  • Loading branch information
rongjiecomputer committed Nov 4, 2018
1 parent 20f5d8b commit fd11082
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 20 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
bazel-*
bazel-*
*.exe
*.lib
*.exp
*.obj
7 changes: 0 additions & 7 deletions java/BUILD

This file was deleted.

1 change: 0 additions & 1 deletion java/WORKSPACE

This file was deleted.

11 changes: 0 additions & 11 deletions java/test.cc

This file was deleted.

5 changes: 5 additions & 0 deletions tensorflow/a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "registry.h"

extern "C" __declspec(dllexport) int foo() { return 23; }

static RegisterCustomCallTarget foo1("foo", reinterpret_cast<void*>(foo));
13 changes: 13 additions & 0 deletions tensorflow/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <cstdio>
#include <cstdint>
#include <cstddef>

#include "registry.h"

int main() {
puts("Hello World");
uintptr_t addr = reinterpret_cast<uintptr_t>(CustomCallTargetRegistry::Global()->Lookup("foo"));
printf("foo: %zu\n", addr);
auto f = reinterpret_cast<int(*)()>(addr);
printf("%d\n", f());
}
18 changes: 18 additions & 0 deletions tensorflow/registry.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "registry.h"

CustomCallTargetRegistry* CustomCallTargetRegistry::Global() {
static auto* registry = new CustomCallTargetRegistry;
return registry;
}

void CustomCallTargetRegistry::Register(const std::string& symbol,
void* address) {
std::lock_guard<std::mutex> lock(mu_);
registered_symbols_[symbol] = address;
}

void* CustomCallTargetRegistry::Lookup(const std::string& symbol) const {
std::lock_guard<std::mutex> lock(mu_);
auto it = registered_symbols_.find(symbol);
return it == registered_symbols_.end() ? nullptr : it->second;
}
21 changes: 21 additions & 0 deletions tensorflow/registry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <mutex>
#include <unordered_map>

class CustomCallTargetRegistry {
public:
static CustomCallTargetRegistry* Global();

void Register(const std::string& symbol, void* address);
void* Lookup(const std::string& symbol) const;

private:
std::unordered_map<std::string, void*> registered_symbols_;
mutable std::mutex mu_;
};

class RegisterCustomCallTarget {
public:
explicit RegisterCustomCallTarget(const std::string& name, void* address) {
CustomCallTargetRegistry::Global()->Register(name, address);
}
};
21 changes: 21 additions & 0 deletions tensorflow/test.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
del *.lib *.obj *.exe *.exp

clang-cl /MD /c /O2 registry.cc
clang-cl /MD /c /O2 main.cpp
clang-cl /MD /c /O2 a.cpp

llvm-lib /out:a.lo.lib a.obj

lld-link /OPT:ICF /OPT:REF /out:ok.exe main.obj /WHOLEARCHIVE:a.lo.lib registry.obj

llvm-lib /llvmlibthin /out:a-thin.lo.lib a.obj
@rem you can get different error in each run
lld-link /OPT:ICF /OPT:REF /out:fail.exe main.obj /WHOLEARCHIVE:a-thin.lo.lib registry.obj
lld-link /OPT:ICF /OPT:REF /out:fail.exe main.obj /WHOLEARCHIVE:a-thin.lo.lib registry.obj
lld-link /OPT:ICF /OPT:REF /out:fail.exe main.obj /WHOLEARCHIVE:a-thin.lo.lib registry.obj
lld-link /OPT:ICF /OPT:REF /out:fail.exe main.obj /WHOLEARCHIVE:a-thin.lo.lib registry.obj
lld-link /OPT:ICF /OPT:REF /out:fail.exe main.obj /WHOLEARCHIVE:a-thin.lo.lib registry.obj
lld-link /OPT:ICF /OPT:REF /out:fail.exe main.obj /WHOLEARCHIVE:a-thin.lo.lib registry.obj
lld-link /OPT:ICF /OPT:REF /out:fail.exe main.obj /WHOLEARCHIVE:a-thin.lo.lib registry.obj
lld-link /OPT:ICF /OPT:REF /out:fail.exe main.obj /WHOLEARCHIVE:a-thin.lo.lib registry.obj
lld-link /OPT:ICF /OPT:REF /out:fail.exe main.obj /WHOLEARCHIVE:a-thin.lo.lib registry.obj

0 comments on commit fd11082

Please sign in to comment.