Skip to content

Recursive template instantations #677

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

Merged
merged 1 commit into from
Aug 22, 2025
Merged

Recursive template instantations #677

merged 1 commit into from
Aug 22, 2025

Conversation

robertoraggi
Copy link
Owner

very WIP.. enough for

extern "C" int printf(const char*, ...);

template <int N>
struct Sum {
  enum Value { value = Sum<N - 1>::value + N };
};

template <>
struct Sum<0> {
  enum Value { value = 0 };
};

auto main() -> int {
  enum { N = 1024 };
  static_assert(Sum<N>::value == (N * (N + 1)) / 2);
  printf("Sum<%d>::value = %d\n", N, Sum<N>::value);
  return 0;
}

cxx x.cc -fcheck -dump-symbols | head -20

namespace
  function int printf(const char*...)
  template class Sum<int>
    parameter object<0, 0, int> N
    enum Value : int
      enumerator Value value
    using Value value
    [specializations]
      class Sum<0>
        enum Value : int
          enumerator Value value = 0
        using Value value
      class Sum<1024>
        enum Value : int
          enumerator Value value = 524800
        using Value value
      class Sum<1023>
        enum Value : int
          enumerator Value value = 523776
        using Value value

cxx x.cc -fcheck -toolchain macos -emit-ir -o -

cxx x.cc -fcheck -toolchain macos -emit-ir -o -
module @x.cc attributes {"cxx.data-layout" = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32", cxx.triple = "arm64-apple-macosx15.0.0", dlti.dl_spec = #dlti.dl_spec<!llvm.ptr<270> = dense<32> : vector<4xi64>, !llvm.ptr<271> = dense<32> : vector<4xi64>, !llvm.ptr<272> = dense<64> : vector<4xi64>, i64 = dense<64> : vector<2xi64>, i128 = dense<128> : vector<2xi64>, !llvm.ptr = dense<64> : vector<4xi64>, i1 = dense<8> : vector<2xi64>, i8 = dense<8> : vector<2xi64>, i16 = dense<16> : vector<2xi64>, i32 = dense<32> : vector<2xi64>, f16 = dense<16> : vector<2xi64>, f64 = dense<64> : vector<2xi64>, f128 = dense<128> : vector<2xi64>, "dlti.endianness" = "little", "dlti.mangling_mode" = "o", "dlti.legal_int_widths" = array<i32: 32, 64>, "dlti.stack_alignment" = 128 : i64, "dlti.function_pointer_alignment" = #dlti.function_pointer_alignment<32, function_dependent = true>>, llvm.data_layout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32", llvm.target_triple = "arm64-apple-macosx15.0.0"} {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.func @main() -> i32 {
    %0 = llvm.mlir.constant(0 : i32) : i32
    %1 = llvm.mlir.constant(524800 : i32) : i32
    %2 = llvm.mlir.constant(1024 : i32) : i32
    %3 = llvm.mlir.addressof @".str1" : !llvm.ptr
    %4 = llvm.mlir.constant(1 : index) : i64
    %5 = llvm.alloca %4 x i32 : (i64) -> !llvm.ptr
    %6 = llvm.call @printf(%3, %2, %1) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i32, i32) -> i32
    llvm.store %0, %5 : i32, !llvm.ptr
    llvm.br ^bb1
  ^bb1:  // pred: ^bb0
    %7 = llvm.load %5 : !llvm.ptr -> i32
    llvm.return %7 : i32
  }
  llvm.mlir.global private constant @".str1"("Sum<%d>::value = %d\0A\00") {addr_space = 0 : i32}
}
cxx x.cc -fcheck -toolchain macos -emit-llvm -o - | lli

Sum<1024>::value = 524800

@robertoraggi robertoraggi force-pushed the fiscal-hare branch 2 times, most recently from a74a773 to cb0e0ce Compare August 22, 2025 18:35
@robertoraggi robertoraggi merged commit cf9426d into main Aug 22, 2025
10 of 18 checks passed
@robertoraggi robertoraggi deleted the fiscal-hare branch August 22, 2025 19:07
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.

1 participant