inkwell icon indicating copy to clipboard operation
inkwell copied to clipboard

internal error: entered unreachable code: The given type is not a basic type. (when looking at the operands of a call)

Open Yoric opened this issue 1 month ago • 4 comments

Describe the Bug I'm trying to use inkwell to inspect some LLVM IR.

To Reproduce

use std::path::Path;

use inkwell::{context::Context, memory_buffer::MemoryBuffer, values::InstructionOpcode};

fn main() {
     // Read the IR file into a memory buffer
    let memory_buffer = MemoryBuffer::create_from_file(Path::new("/tmp/test.ll")).unwrap();

    let context = Context::create();
    // Parse the IR into a module
    let module = context
        .create_module_from_ir(memory_buffer).unwrap();

    let main = module.get_function("main").unwrap();
    for bb in main.get_basic_blocks() {
        for instruction in bb.get_instructions() {
            eprintln!("Instruction: {:?}", instruction);
            if instruction.get_opcode() == InstructionOpcode::Call {
                eprintln!("Looking at {} operands", instruction.get_num_operands());
                for (i, operand) in instruction.get_operands().enumerate() {
                    eprintln!("Operand {i}: {:?}", operand);
                }
            }
        }
    }
}

I have applied this to the LLVM IR for the following file:

#include <fcntl.h>
#include <unistd.h>

int main() {
    int fd = open("/tmp/test.txt", O_RDONLY);
    if (fd >= 0) {
        char buffer[256];
        read(fd, buffer, sizeof(buffer));
        close(fd);
    }
    return 0;
}

(corresponding .ll)

In the first call, this panics with

internal error: entered unreachable code: The given type is not a basic type.

Expected Behavior I expected to be able to look at all the operands of the call.

LLVM Version (please complete the following information):

  • LLVM Version: [e.g. 6.0] llvm18-1
  • Inkwell Branch Used: 0.7.1

Desktop (please complete the following information):

  • OS: [e.g. Ubuntu 16.04, Windows 10] macOS 12.7.6.

Yoric avatar Nov 24 '25 17:11 Yoric

Looking at it more in depth, the error shows up at the following IR:

  call void @llvm.dbg.declare(metadata i32* %2, metadata !16, metadata !DIExpression()), !dbg !17

the panic is raised because the arguments are metadata, which cannot be converted into BasicValueEnum. If I skip llvm.dbg.*, other functions work nicely.

Yoric avatar Nov 25 '25 18:11 Yoric

I think we have a MetadataBasicValueEnum which is probably want wherever it's failing

TheDan64 avatar Nov 26 '25 00:11 TheDan64

Let me see if I can write a PR for this.

Yoric avatar Nov 26 '25 07:11 Yoric

PR opened.

Yoric avatar Nov 27 '25 07:11 Yoric