Skip to content
This repository has been archived by the owner on Nov 25, 2022. It is now read-only.

Commit

Permalink
[JIT] Force finalization of JITed code, expose sf/hf runtime functions (
Browse files Browse the repository at this point in the history
apache#12187)

* [JIT] Force finalization of JITed code, expose sf/hf runtime functions

Code that handles fp16 and fp32 may end up calling builtins that do the
conversions between these types. LLVM emits calls to __truncsfhf2, and
__extendhfsf2, which are not present in TVM or TVM runtime. This creates
two problems:
- Problem 1: JITed code that does the conversions will fail because it
  calls non-existent functions.
Adding these functions to libtvm.so/libtvm_runtime.so solves this part,
but there is another issue:
- Problem 2: JITed code may still not call these functions, because the
generated object may not be fully resolved.
To force full resolution, try to obtain an address of a non-existent
symbol.

* Restart CI
  • Loading branch information
Krzysztof Parzyszek authored and xinetzone committed Nov 25, 2022
1 parent 485afbb commit 5c3bef4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/tvm/runtime/builtin_fp16.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
extern "C" {
TVM_DLL uint16_t __gnu_f2h_ieee(float);
TVM_DLL float __gnu_h2f_ieee(uint16_t);
TVM_DLL uint16_t __truncsfhf2(float v);
TVM_DLL float __extendhfsf2(uint16_t v);
}

#endif // TVM_RUNTIME_BUILTIN_FP16_H_
3 changes: 3 additions & 0 deletions src/runtime/builtin_fp16.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ TVM_DLL float __gnu_h2f_ieee(uint16_t a) {
}

#endif

TVM_DLL uint16_t __truncsfhf2(float v) { return __gnu_f2h_ieee(v); }
TVM_DLL float __extendhfsf2(uint16_t v) { return __gnu_h2f_ieee(v); }
}
6 changes: 6 additions & 0 deletions src/target/llvm/llvm_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ class LLVMModuleNode final : public runtime::ModuleNode {
}
runtime::InitContextFunctions(
[this](const char* name) { return reinterpret_cast<void*>(GetGlobalAddr(name)); });
// There is a problem when a JITed function contains a call to a runtime function.
// The runtime function (e.g. __truncsfhf2) may not be resolved, and calling it will
// lead to a runtime crash.
// Do name lookup on a symbol that doesn't exist. This will force MCJIT to finalize
// all loaded objects, which will resolve symbols in JITed code.
ee_->getFunctionAddress("__some_name_that_hopefully_doesnt_exist__b49f8aaade5877eaba7583b91");
}

// Get global address from execution engine.
Expand Down

0 comments on commit 5c3bef4

Please sign in to comment.