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

compiler-rt linkage needed for certain builtins (e.g. truncate double to half type) #834

Open
jvesely opened this issue Apr 15, 2022 · 2 comments
Labels
feature_request Feature Request

Comments

@jvesely
Copy link
Contributor

jvesely commented Apr 15, 2022

a simple example is fp conversion from fp64 to fp16:

fnty = ir.FunctionType(int16, (double,))

module = ir.Module(name=__file__)
func = ir.Function(module, fnty, name="fptest")

block = func.append_basic_block(name="entry")
builder = ir.IRBuilder(block)
a, = func.args
result = builder.fptrunc(a, half, name="res")
result = builder.bitcast(result, int16)
builder.ret(result)

the generated assembly looks ok:

	.text
	.file	"<string>"
	.globl	fptest
	.p2align	4, 0x90
	.type	fptest,@function
fptest:
	.cfi_startproc
	movabsq	$__truncdfhf2, %rax
	jmpq	*%rax
.Lfunc_end0:
	.size	fptest, .Lfunc_end0-fptest
	.cfi_endproc

	.section	".note.GNU-stack","",@progbits

however, when inspecting the function in gdb after receiving sigsegv:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) disassemble fptest 
Dump of assembler code for function fptest:
   0x00007fffea010000 <+0>:	movabs $0x0,%rax
   0x00007fffea01000a <+10>:	jmp    *%rax
End of assembler dump.

This looks like a linking failure. the necessary __truncdfhf2 function should be in LLVM's compiler-rt library.

jvesely added a commit to jvesely/PsyNeuLink that referenced this issue Apr 15, 2022
Add tests.
Conversion from double -> half is not accurate,
because of an issue with a call to runtime library [0].

[0] numba/llvmlite#834

Signed-off-by: Jan Vesely <jan.vesely@rutgers.edu>
jvesely added a commit to jvesely/PsyNeuLink that referenced this issue Apr 15, 2022
Add tests.
Conversion from double -> half is not accurate,
because of an issue with a call to runtime library [0].

[0] numba/llvmlite#834

Signed-off-by: Jan Vesely <jan.vesely@rutgers.edu>
@stuartarchibald
Copy link
Contributor

Thanks for the report. I think this will require making compiler-rt available to llvmlite, which also involves building it as part of the llvmdev package. IIRC this is on the long term to-do list as it also helps supply the tooling for things like code coverage.

MWR for the above:

import llvmlite.binding as llvm
from llvmlite import ir

int16 = ir.IntType(16)
double = ir.DoubleType()
half = ir.HalfType()

fnty = ir.FunctionType(int16, (double,))

module = ir.Module(name=__file__)
func = ir.Function(module, fnty, name="fptest")

block = func.append_basic_block(name="entry")
builder = ir.IRBuilder(block)
a, = func.args
result = builder.fptrunc(a, half, name="res")
result = builder.bitcast(result, int16)
builder.ret(result)
print(module)

llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()


target = llvm.Target.from_default_triple()
target_machine = target.create_target_machine()
llmod = llvm.parse_assembly(str(module))
llmod.verify()
engine = llvm.create_mcjit_compiler(llmod, target_machine)
engine.finalize_object()
engine.run_static_constructors()

print(target_machine.emit_assembly(llmod))

@stuartarchibald stuartarchibald added the feature_request Feature Request label Apr 19, 2022
@stuartarchibald stuartarchibald changed the title code generating runtime library calls results in segfault compiler-rt linkage needed for certain builtins (e.g. truncate double to half type) Apr 19, 2022
@stuartarchibald
Copy link
Contributor

Note: Issue title updated to reflect required changes/help with searching/reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature_request Feature Request
Projects
None yet
Development

No branches or pull requests

2 participants