Skip to content

Conversation

@htyu
Copy link
Contributor

@htyu htyu commented Aug 14, 2023

As a follow up to #197, this change pre-lowers high-level CIR for global initializers.

High-level CIR:

  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }

After pre-lowering:

   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }

There is still work to be done to fully lower to LLVM. E.g, add llvm.global_ctors global to list all module initializers like _GLOBAL__sub_I_static. This will be handled in a separate change.

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.

Can you split this into two PRs? One that adds the basically empty pass and the other that adds the first set of functionality? It will make it easier to review!

htyu added a commit that referenced this pull request Aug 15, 2023
@htyu htyu force-pushed the staticinit-lowering branch from b85eabe to 062fcc8 Compare August 15, 2023 20:21
@htyu
Copy link
Contributor Author

htyu commented Aug 15, 2023

Can you split this into two PRs? One that adds the basically empty pass and the other that adds the first set of functionality? It will make it easier to review!

Done. This PR now only contains changes to pre-lower global initializers.

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.

Awesome, LGTM with minor changes.


void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
auto &ctorRegion = op.getCtorRegion();
if (!ctorRegion.empty()) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you instead early return if ctorRegion.empty() is true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add a parallel dtorRegion check later on, so the check would probably look as is. So make sense to leave it as is?

Copy link
Member

Choose a reason for hiding this comment

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

Ok, sounds good!

@htyu htyu force-pushed the staticinit-lowering branch from 062fcc8 to 1215cc8 Compare August 16, 2023 00:05

void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
auto &ctorRegion = op.getCtorRegion();
if (!ctorRegion.empty()) {
Copy link
Member

Choose a reason for hiding this comment

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

Ok, sounds good!

@htyu htyu merged commit f318211 into llvm:main Aug 16, 2023
@htyu htyu deleted the staticinit-lowering branch August 16, 2023 00:16
lanza pushed a commit that referenced this pull request Oct 27, 2023
lanza pushed a commit that referenced this pull request Oct 27, 2023
…zers (#235)

As a follow up to #197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```


There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
lanza pushed a commit that referenced this pull request Dec 20, 2023
lanza pushed a commit that referenced this pull request Dec 20, 2023
…zers (#235)

As a follow up to #197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```


There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
lanza pushed a commit that referenced this pull request Jan 29, 2024
lanza pushed a commit that referenced this pull request Jan 29, 2024
…zers (#235)

As a follow up to #197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```


There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
lanza pushed a commit that referenced this pull request Mar 23, 2024
lanza pushed a commit that referenced this pull request Mar 23, 2024
…zers (#235)

As a follow up to #197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```


There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
eZWALT pushed a commit to eZWALT/clangir that referenced this pull request Mar 24, 2024
…zers (llvm#235)

As a follow up to llvm#197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```


There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
eZWALT pushed a commit to eZWALT/clangir that referenced this pull request Mar 24, 2024
eZWALT pushed a commit to eZWALT/clangir that referenced this pull request Mar 24, 2024
…zers (llvm#235)

As a follow up to llvm#197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```


There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
lanza pushed a commit that referenced this pull request Apr 29, 2024
lanza pushed a commit that referenced this pull request Apr 29, 2024
…zers (#235)

As a follow up to #197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```

There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
lanza pushed a commit that referenced this pull request Apr 29, 2024
lanza pushed a commit that referenced this pull request Apr 29, 2024
…zers (#235)

As a follow up to #197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```

There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
eZWALT pushed a commit to eZWALT/clangir that referenced this pull request Apr 29, 2024
eZWALT pushed a commit to eZWALT/clangir that referenced this pull request Apr 29, 2024
…zers (llvm#235)

As a follow up to llvm#197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```


There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
lanza pushed a commit that referenced this pull request Apr 29, 2024
lanza pushed a commit that referenced this pull request Apr 29, 2024
…zers (#235)

As a follow up to #197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```

There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
pysuxing pushed a commit to pysuxing/llvm-project that referenced this pull request Jul 17, 2024
bruteforceboy pushed a commit to bruteforceboy/clangir that referenced this pull request Oct 2, 2024
…zers (llvm#235)

As a follow up to llvm#197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```

There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
Hugobros3 pushed a commit to shady-gang/clangir that referenced this pull request Oct 2, 2024
Hugobros3 pushed a commit to shady-gang/clangir that referenced this pull request Oct 2, 2024
…zers (llvm#235)

As a follow up to llvm#197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```

There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
keryell pushed a commit to keryell/clangir that referenced this pull request Oct 19, 2024
keryell pushed a commit to keryell/clangir that referenced this pull request Oct 19, 2024
…zers (llvm#235)

As a follow up to llvm#197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```

There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
lanza pushed a commit that referenced this pull request Nov 5, 2024
lanza pushed a commit that referenced this pull request Nov 5, 2024
…zers (#235)

As a follow up to #197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```

There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
lanza pushed a commit that referenced this pull request Mar 18, 2025
lanza pushed a commit that referenced this pull request Mar 18, 2025
…zers (#235)

As a follow up to #197, this change
pre-lowers high-level CIR for global initializers.

High-level CIR:

```
  cir.global "private" internal @_ZL8__ioinit = ctor : !ty_22class2EInit22 {
    %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22> loc(#loc8)
    %1 = cir.const(#true) : !cir.bool loc(#loc5)
    cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> () loc(#loc6)
 }
```

After pre-lowering:

```
   cir.global "private" internal @_ZL8__ioinit =  #cir.zero : !ty_22class2EInit22 {ast = #cir.vardecl.ast}

   cir.func internal private @__cxx_global_var_init() {
     %0 = cir.get_global @_ZL8__ioinit : cir.ptr <!ty_22class2EInit22>
     %1 = cir.const(#true) : !cir.bool
     cir.call @_ZN4InitC1Eb(%0, %1) : (!cir.ptr<!ty_22class2EInit22>, !cir.bool) -> ()
     cir.return
  }

   cir.func private @_GLOBAL__sub_I_static.cpp() {
     cir.call @__cxx_global_var_init() : () -> ()
     cir.return
  }
```

There is still work to be done to fully lower to LLVM. E.g, add
`llvm.global_ctors` global to list all module initializers like
`_GLOBAL__sub_I_static`. This will be handled in a separate change.
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