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

Default constructor isn't optimised out during midend LLVM. #1098

Open
willir opened this issue Nov 10, 2024 · 4 comments
Open

Default constructor isn't optimised out during midend LLVM. #1098

willir opened this issue Nov 10, 2024 · 4 comments
Assignees
Labels
bug Something isn't working IR difference A difference in ClangIR-generated LLVM IR that could complicate reusing original CodeGen tests

Comments

@willir
Copy link
Contributor

willir commented Nov 10, 2024

If you compile the following code:

struct Foo {
  int get_num_field(int v) { return v; }
};

int get_num(int a) {
  return Foo().get_num_field(a);
}

with: clang -O3 -fclangir -S -emit-llvm

The output for get_num looks like:

; Function Attrs: nounwind
define dso_local i32 @_Z7get_numi(i32 returned %0) local_unnamed_addr #0 !dbg !7 {
  %2 = alloca %struct.Foo, align 1, !dbg !8
  call void @_ZN3FooC1Ev(ptr nonnull %2), !dbg !9
  ret i32 %0, !dbg !8
}

While without -fclangir it looks like this:

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i32 @_Z8get_num2i(i32 noundef returned %a) local_unnamed_addr #0 {
entry:
  ret i32 %a
}

Notice that the call to the constructor is not optimized out in the clangir mode
Here is the link to godbolt that reproduces the issue: https://godbolt.org/z/bTos9qx4d

I think it happens because the default constructor is declared and not defined in the clangir mode.

Is it a conscious decision or a bug?

@bcardosolopes bcardosolopes self-assigned this Nov 11, 2024
@bcardosolopes
Copy link
Member

Is it a conscious decision or a bug?

Both, we conscious map it but we should optimize it out in LoweringPrepare when it's trivial. Let me put this on my queue! Thanks

@willir
Copy link
Contributor Author

willir commented Nov 11, 2024

Can I ask the reason for having different behavior than in the current clang?
Or it is not different?

@bcardosolopes
Copy link
Member

bcardosolopes commented Nov 12, 2024

Can I ask the reason for having different behavior than in the current clang?

We want to track source semantics of when objects get constructed and destroyed, more interesting for some static analysys, but for later codegen purposes it should be removed. So it's not different in the sense that you should expect the same LLVM IR you get through OG clang (it's a bug now).

@bcardosolopes bcardosolopes added bug Something isn't working IR difference A difference in ClangIR-generated LLVM IR that could complicate reusing original CodeGen tests labels Nov 12, 2024
@willir
Copy link
Contributor Author

willir commented Nov 12, 2024

I see, thanks for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working IR difference A difference in ClangIR-generated LLVM IR that could complicate reusing original CodeGen tests
Projects
None yet
Development

No branches or pull requests

2 participants