Skip to content

Trivial duplicate code paths are not always merged #26496

Closed
@Tobba

Description

@Tobba

Long winded test case using libcore: (build with --crate-type staticlib and --emit asm)

#![feature(no_std, lang_items, asm, core)]
#![no_std]

use core::fmt;

#[macro_use]
extern crate core;

#[lang="panic_fmt"]
extern fn panic_fmt(msg: &fmt::Arguments, file: &'static str, line: u32) -> ! {
    loop { }
}

#[lang="stack_exhausted"]
extern fn stack_exhausted() {
    loop { }
}

#[lang="eh_personality"]
extern fn eh_personality() {
    loop { }
}

#[inline(always)]
fn foo() {
    panic!("Das Ist Nümberwang!");
}

fn black_box<T>(mut value: T) -> T {
    unsafe {
        asm!("" :: "r"(&mut value) :: "volatile");
    }
    value
}

pub fn main() {
    match black_box(5) {
        1 => foo(),
        48 => foo(),
        92 => foo(),
        23 => foo(),
        4 => foo(),
        70 => foo(),
        29 => foo(),
        36 => foo(),
        34 => foo(),
        58 => foo(),
        _ => { }
    }
}

This duplicates the following 3 times in the resulting code:

    leaq    _ZN3foo14_MSG_FILE_LINE20h7545777ae10911d4MaaE(%rip), %rdi
    callq   _ZN9panicking5panic20hefc2bd49b5114e0db7BE@PLT

This may not seem too bad, but in real world cases you can end up with something on the order of this: https://gist.github.com/Tobba/ab10d5644d3d4ef97917

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions