Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed allocas insertion point for LLVM backend #663

Merged
merged 2 commits into from
May 20, 2021

Conversation

georgemitenkov
Copy link
Collaborator

@georgemitenkov georgemitenkov commented May 20, 2021

With this PR alloca instructions are always inserted in the beginning of the function entry block. This is done to avoid them in the while or for loops, where allocations per iteration cause stack overflow (if the IR is not optimized).

Example:

/// loop.mod
PROCEDURE loop(a, b) {
    WHILE (a > 0) {
        LOCAL x 
        x = b
    }
}

Before, this code would produce the following LLVM IR:

define i32 @loop(double %a1, double %b2) {
  %a = alloca double, align 8
  store double %a1, double* %a, align 8
  %b = alloca double, align 8
  store double %b2, double* %b, align 8
  %ret_loop = alloca i32, align 4
  store i32 0, i32* %ret_loop, align 4
  br label %1

1:                                                ; preds = %4, %0
  %2 = load double, double* %a, align 8
  %3 = fcmp ogt double %2, 0.000000e+00
  br i1 %3, label %4, label %6

4:                                                ; preds = %1
  ; x is allocated inside the loop body - this is bad!
  %x = alloca double, align 8
  %5 = load double, double* %b, align 8
  store double %5, double* %x, align 8
  br label %1

6:                                                ; preds = %1
  %7 = load i32, i32* %ret_loop, align 4
  ret i32 %7
}

Now, the result is:

define i32 @loop(double %a1, double %b2) {
  %a = alloca double, align 8
  %b = alloca double, align 8
  %ret_loop = alloca i32, align 4
  ; x is not allocated inside the loop body!
  %x = alloca double, align 8
  store double %a1, double* %a, align 8
  store double %b2, double* %b, align 8
  store i32 0, i32* %ret_loop, align 4
  br label %1

1:                                                ; preds = %4, %0
  %2 = load double, double* %a, align 8
  %3 = fcmp ogt double %2, 0.000000e+00
  br i1 %3, label %4, label %6

4:                                                ; preds = %1
  %5 = load double, double* %b, align 8
  store double %5, double* %x, align 8
  br label %1

6:                                                ; preds = %1
  %7 = load i32, i32* %ret_loop, align 4
  ret i32 %7
}

@bbpbuildbot
Copy link
Collaborator

Can one of the admins verify this patch?

Copy link
Contributor

@pramodk pramodk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👌

src/codegen/llvm/llvm_ir_builder.cpp Outdated Show resolved Hide resolved
@pramodk pramodk merged commit 1461d8f into llvm May 20, 2021
@pramodk pramodk deleted the georgemitenkov/llvm-allocas branch May 20, 2021 21:19
pramodk pushed a commit that referenced this pull request Mar 8, 2022
* With this PR alloca instructions are always inserted in the beginning
   of the function entry block. This is done to avoid them in the while or
   for loops, where allocations per iteration cause stack overflow
   (if the IR is not optimized).
* Insertion point for allocas is the enetry block now

See #653
iomaganaris pushed a commit that referenced this pull request May 10, 2022
* With this PR alloca instructions are always inserted in the beginning
   of the function entry block. This is done to avoid them in the while or
   for loops, where allocations per iteration cause stack overflow
   (if the IR is not optimized).
* Insertion point for allocas is the enetry block now

See #653
iomaganaris pushed a commit that referenced this pull request May 12, 2022
* With this PR alloca instructions are always inserted in the beginning
   of the function entry block. This is done to avoid them in the while or
   for loops, where allocations per iteration cause stack overflow
   (if the IR is not optimized).
* Insertion point for allocas is the enetry block now

See #653
iomaganaris pushed a commit that referenced this pull request Sep 15, 2022
* With this PR alloca instructions are always inserted in the beginning
   of the function entry block. This is done to avoid them in the while or
   for loops, where allocations per iteration cause stack overflow
   (if the IR is not optimized).
* Insertion point for allocas is the enetry block now

See #653
iomaganaris pushed a commit that referenced this pull request Sep 15, 2022
* With this PR alloca instructions are always inserted in the beginning
   of the function entry block. This is done to avoid them in the while or
   for loops, where allocations per iteration cause stack overflow
   (if the IR is not optimized).
* Insertion point for allocas is the enetry block now

See #653
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants