Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLVM Struct Return Optimization

I'm wondering why LLVM fails to optimize the following IR code (using the PassManagerBuilder with optimisation set to '3', and also using LLVM's 'opt' tool):

%GenericStruct = type { i32 }

define void @makeGenericStructOuter(%GenericStruct* noalias nocapture sret) {
entry:
  %1 = alloca %GenericStruct
  call void @makeGenericStructInner(%GenericStruct* %1)
  %2 = load %GenericStruct* %1
  store %GenericStruct %2, %GenericStruct* %0
  ret void
}

declare void @makeGenericStructInner(%GenericStruct* noalias nocapture sret)

The expected code is:

%GenericStruct = type { i32 }

define void @makeGenericStructOuter(%GenericStruct* noalias nocapture sret) {
entry:
  call void @makeGenericStructInner(%GenericStruct* %0)
  ret void
}

declare void @makeGenericStructInner(%GenericStruct* noalias nocapture sret)

Are there simply no optimizations currently available to handle this case? Or am I failing to produce (this code is generated from a front-end I'm developing) the right IR that would allow optimization?

Before it's suggested, I can't produce code that returns by value since these functions must be callable from other modules/libraries that don't know the size or contents of 'GenericStruct' (and they would locally declare 'TestClass' as 'struct opaque').

like image 490
Stephen Cross Avatar asked Oct 23 '12 21:10

Stephen Cross


People also ask

Is LLVM optimized?

LLVM features powerful intermodular optimizations which can be used at link time. Link Time Optimization (LTO) is another name for intermodular optimization when performed during the link stage.

What is LLVM optimization?

DESCRIPTION. The opt command is the modular LLVM optimizer and analyzer. It takes LLVM source files as input, runs the specified optimizations or analyses on it, and then outputs the optimized file.

What is mem2reg?

-mem2reg : Promote Memory to Register This file promotes memory references to be register references. It promotes alloca instructions which only have loads and stores as uses.

What is clang LTO?

LTO (Link Time Optimization) achieves better runtime performance through whole-program analysis and cross-module optimization. However, monolithic LTO implements this by merging all input into a single module, which is not scalable in time or memory, and also prevents fast incremental compiles.


1 Answers

If you belive that optimization should take place, report a bug in LLVM Bug Tracker. LLVM developers are usually very happy and interested when reporting optimization opportunities missed.

like image 73
Paweł Bylica Avatar answered Sep 26 '22 02:09

Paweł Bylica