bioblend icon indicating copy to clipboard operation
bioblend copied to clipboard

InstructionBuilder.SDiv creates UDiv instead of SDiv

Open PennyMew opened this issue 1 year ago • 1 comments

InstructionBuilder.SDiv creates UDiv instead of SDiv.

Here's some example code to demonstrate the issue.

public void SDivBug()
{
    Context context = new Context();
    BitcodeModule module = context.CreateBitcodeModule();
    
    // We're going to create the LLVM equivalent of this C function:
    //      int test(int x)
    //      {
    //          return x / -3;
    //      }

    IFunctionType function_type = context.GetFunctionType(context.Int32Type, new ITypeRef[]{context.Int32Type});
    IrFunction function = module.CreateFunction("test", function_type);
    BasicBlock block = function.AppendBasicBlock("entry");
    InstructionBuilder builder = new InstructionBuilder(block);
    Value lhs = function.Parameters[0];
    Value rhs = context.CreateConstant(-3);

    // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    Value sdiv = builder.SDiv(lhs, rhs); // <== Creating SDiv
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    builder.Return(sdiv);

    if (!module.Verify(out string msg))
    {
        Console.WriteLine("module.Verify gave error: {0}", msg);
    }
    else
    {
        Console.WriteLine("module.Verify was okay");
    }

    Console.WriteLine(module.WriteToString());
}

Running this gives the following output:

module.Verify was okay

define i32 @test(i32 %0) {
entry:
  %1 = udiv i32 %0, -3
  ret i32 %1
}

Note that we have udiv instead of sdiv.

It looks like the following part of the InstructionBuilder code is selecting the wrong op builder:

https://github.com/UbiquityDotNET/Llvm.NET/blob/aabdd51f52a5a7ea634a2d0beeed42e7a6f2132a/src/Ubiquity.NET.Llvm/Instructions/InstructionBuilder.cs#L219-L229

PennyMew avatar Sep 15 '22 18:09 PennyMew

I can confirm that is a bug, (and an easy enough fix) were you planning on submitting a PR to fix it?

smaillet avatar Oct 14 '22 15:10 smaillet