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

[CIR][CodeGen] Fix std::ofstream fail during CodeGen #1437

Merged
merged 1 commit into from
Mar 5, 2025

Conversation

bruteforceboy
Copy link
Contributor

Currently, the following code snippet fails during CodeGen, using clang++ tmp.cpp -fclangir -Xclang -emit-cir -S:

#include <fstream>

void foo(const char *path) {
  std::ofstream fout1(path);
  fout1 << path;
  std::ofstream fout2(path);
  fout2 << path;
}

It fails with:

error: 'cir.yield' op expects parent op to be one of 'cir.if, cir.scope, cir.switch, cir.while, cir.for, cir.await, cir.ternary, cir.global, cir.do, cir.try, cir.array.ctor, cir.array.dtor, cir.call, cir.case'

The relevant part of the CIR dump before verification looks like:

    "cir.br"()[^bb1] : () -> ()
  ^bb1:  // pred: ^bb0
    "cir.yield"() : () -> ()
    "cir.return"() : () -> ()
  }) : () -> ()

Two things are wrong: the YieldOp has cir.func as a parent and there is a cir.return too. These come right after the second destructor for basic_ofstream.

This PR fixes this by checking if there is a terminator and removing (if it exists) before adding an implicit return. I have also added a test that mimics the behavior of std::basic_ofstream.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

Thank you!

@bcardosolopes bcardosolopes merged commit 2d36018 into llvm:main Mar 5, 2025
7 checks passed
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 this pull request may close these issues.

2 participants