Skip to content

LLVM converts integer types to floats #194

@AnErrupTion

Description

@AnErrupTion

Hey!

I'm making a compiler which will use LLVM as its backend. But while I was doing some tests, I saw that LLVM, for some reason, was transforming any integer types (i8, i16, i32 or i64) to floats (more precisely, ppc_fp128). But some other types like double work and are not converted to floats (since doubles can hold decimals already).

What I don't understand is that I'm not using any decimal values, and it is still converting them into floats, which actually breaks my code.

Here is a sample demo which does this behavior:

unsafe
{
    LLVMModuleRef module = LLVM.ModuleCreateWithName((sbyte*)0);
    LLVMBuilderRef builder = LLVM.CreateBuilder();

    const string targetStr = "x86_64-unknown-none";

    LLVM.InitializeX86TargetInfo();
    LLVM.InitializeX86Target();
    LLVM.InitializeX86TargetMC();
    LLVM.InitializeX86AsmParser();
    LLVM.InitializeX86AsmPrinter();

    module.Target = targetStr;

    var target = LLVMTargetRef.GetTargetFromTriple(targetStr);
    var machine = target.CreateTargetMachine(targetStr, "generic", "", LLVMCodeGenOptLevel.LLVMCodeGenLevelNone,
        LLVMRelocMode.LLVMRelocStatic, LLVMCodeModel.LLVMCodeModelKernel);

    LLVM.SetModuleDataLayout(module, machine.CreateTargetDataLayout());

    var function = module.AddFunction("main", LLVM.FunctionType(LLVM.VoidType(), null, 0, 0));
    var stack = new Stack<LLVMValueRef>();

    LLVM.SetLinkage(function, LLVMLinkage.LLVMExternalLinkage);
    builder.PositionAtEnd(function.AppendBasicBlock("entry"));

    // Simulating what the stack would look like before Add
    stack.Push(LLVM.ConstReal(LLVM.Int32Type(), 8));
    stack.Push(LLVM.ConstReal(LLVM.Int32Type(), 4));

    var value2 = stack.Pop();
    var value1 = stack.Pop();

    stack.Push(builder.BuildAdd(value1, value2));

    fixed (LLVMOpaqueType** ptr = new[] { LLVM.Int32Type() })
    {
        var tmp = module.AddFunction("tmp", LLVM.FunctionType(LLVM.VoidType(), ptr, 1, 0));
        LLVM.SetLinkage(tmp, LLVMLinkage.LLVMExternalLinkage);
        builder.BuildCall(tmp, new[] { stack.Pop() /* This should be the result of the previous Add instruction */ });
    }

    builder.BuildRetVoid();

    LLVM.DumpModule(module);
}

Here is the LLVM IR output:

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-none"

define void @main() {
entry:
  call void @tmp(ppc_fp128 add (ppc_fp128 0xM40200000000000000000000000000000, ppc_fp128 0xM40100000000000000000000000000000))
  ret void
}

declare void @tmp(i32 %0)

I'm not quite sure if this is a bug, or if I'm using LLVM(Sharp) wrong here. Any help would be greatly appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions