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

Loop body should be emitted in different scope #1218

Closed
Lancern opened this issue Dec 9, 2024 · 2 comments · Fixed by #1221
Closed

Loop body should be emitted in different scope #1218

Lancern opened this issue Dec 9, 2024 · 2 comments · Fixed by #1221

Comments

@Lancern
Copy link
Member

Lancern commented Dec 9, 2024

In the comments of CIRGenStmt.cpp:

// https://en.cppreference.com/w/cpp/language/for
// While in C++, the scope of the init-statement and the scope of
// statement are one and the same, in C the scope of statement is
// nested within the scope of init-statement.
bool useCurrentScope =
CGM.getASTContext().getLangOpts().CPlusPlus ? true : false;

I believe the comment's statement is actually wrong. The referenced page says:

  • The scope of init-statement and the scope of condition are the same.
  • The scope of statement and the scope of expression are disjoint and nested within the scope of init-statement and condition.

Thus in C++ the scope of statement should be in a scope nested within the scope of init-statement, but not in the same scope with init-statement.

For the following C++ code:

void test() {
  for (int i = 0; i < 10; ++i) {
    int x = 42;
    use(x);
  }
}

ClangIR currently emits the following CIR:

cir.func @_Z4testv() {
  cir.scope {
    %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["i", init]
    %1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init]
    // ...
    cir.for : cond {
      // ...
    } body {
      // ...
    } step {
      // ...
    }
  }
  cir.return
}

I actually expect it to be:

cir.func @_Z4testv() {
  cir.scope {
    %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["i", init]
    // ...
    cir.for : cond {
      // ...
    } body {
      cir.scope {
        %1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init]
        // ...
      }
    } step {
      // ...
    }
  }
  cir.return
}

Note the difference of the places of the alloca operation for local variable x.

@bcardosolopes
Copy link
Member

Makes sense to me!

@Lancern
Copy link
Member Author

Lancern commented Dec 10, 2024

The same problem also exists for while loops. For the following C++ code:

void test() {
  while (int x = foo()) {
    int y = foo();
    use(x, y);
  }
}

ClangIR currently emits:

cir.func @_Z4testv() extra(#fn_attr) {
  cir.scope {
    %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init]
    %1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["y", init]
    cir.while {
      // ...
    } do {
      // ...
    }
  }
  cir.return
}

I actually expect it to be:

cir.func @_Z4testv() extra(#fn_attr) {
  cir.scope {
    %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init]
    cir.while {
      // ...
    } do {
      cir.scope {
        %1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["y", init]
        // ...
      }
    }
  }
  cir.return
}

A somehow unrelated issue is the alloca operation for x, which is a variable declared at the condition part of the while loop. The standard says its lifetime should end at the end of every iteration, and upon each iteration it should be re-created as a fresh object, but seems like the generated CIR does not reflect this fact.

Lancern added a commit to Lancern/clangir that referenced this issue Dec 10, 2024
This patch puts for-loop body, while-loop body, and do-while-loop body in nested
scopes. Allocas in the loop body are now push down to the nested scope.

Resolve llvm#1218 .
@Lancern Lancern changed the title For loop body should be emitted in different scope Loop body should be emitted in different scope Dec 10, 2024
Lancern added a commit to Lancern/clangir that referenced this issue Dec 15, 2024
This patch puts for-loop body, while-loop body, and do-while-loop body in nested
scopes. Allocas in the loop body are now push down to the nested scope.

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

Successfully merging a pull request may close this issue.

2 participants