File: cxx20-decomposition.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (48 lines) | stat: -rw-r--r-- 1,990 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// RUN: %clang_cc1 -std=c++20 -triple x86_64-linux-gnu -emit-llvm %s -o - | FileCheck %s

struct S {
  int i;
  int j;
};

int f() {
  auto [i, j] = S{1, 42};
  return [&i, j] {
    return i + j;
  }();
}

// Ensures the representation of the lambda, the order of the
// 1st 2nd don't matter except for ABI-esque things, but make sure
// that the ref-capture is a ptr, and 'j' is captured by value.
// CHECK: %[[LAMBDA_TY:.+]] = type <{ ptr, i32, [4 x i8] }>

// Check the captures themselves.
// CHECK: define{{.*}} i32 @_Z1fv()
// CHECK: %[[BINDING:.+]] = alloca %struct.S
// CHECK: %[[LAMBDA:.+]] = alloca %[[LAMBDA_TY]]

// Copy a pointer to the binding, for reference capture.
// CHECK: %[[LAMBDA_CAP_PTR:.+]] = getelementptr inbounds %[[LAMBDA_TY]], ptr %[[LAMBDA]], i32 0, i32 0
// CHECK: %[[BINDING_PTR:.+]] = getelementptr inbounds %struct.S, ptr %[[BINDING]], i32 0, i32 0
// CHECK: store ptr %[[BINDING_PTR]], ptr %[[LAMBDA_CAP_PTR]]

// Copy the integer from the binding, for copy capture.
// CHECK: %[[LAMBDA_CAP_INT:.+]] = getelementptr inbounds %[[LAMBDA_TY]], ptr %[[LAMBDA]], i32 0, i32 1
// CHECK: %[[PTR_TO_J:.+]] = getelementptr inbounds %struct.S, ptr %[[BINDING]], i32 0, i32 1
// CHECK: %[[J_COPY:.+]] = load i32, ptr %[[PTR_TO_J]]
// CHECK: store i32 %[[J_COPY]], ptr %[[LAMBDA_CAP_INT]]

// Ensure the captures are properly extracted in operator().
// CHECK: define{{.*}} i32 @"_ZZ1fvENK3$_0clEv"
// CHECK: %[[THIS_ADDR:.+]] = alloca ptr
// CHECK: %[[THIS_PTR:.+]] = load ptr, ptr %[[THIS_ADDR]]

// Load 'i', passed by reference.
// CHECK: %[[LAMBDA_GEP_TO_PTR:.+]] = getelementptr inbounds %[[LAMBDA_TY]], ptr %[[THIS_PTR]], i32 0, i32 0
// CHECK: %[[I_PTR:.+]] = load ptr, ptr %[[LAMBDA_GEP_TO_PTR]]
// CHECK: %[[I_VALUE:.+]] = load i32, ptr %[[I_PTR]]

// Load the 'j', passed by value.
// CHECK: %[[LAMBDA_GEP_TO_INT:.+]] = getelementptr inbounds %[[LAMBDA_TY]], ptr %[[THIS_PTR]], i32 0, i32 1
// CHECK: %[[J_VALUE:.+]] = load i32, ptr %[[LAMBDA_GEP_TO_INT]]