Skip to content

Commit 57872dd

Browse files
committed
Test: Unorderded Map Access
This demonstrates two issues with `std::unordered_map`: - `at()` fails to compile - `operator[]` has a memory leak
1 parent f69db36 commit 57872dd

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O0 %s -S -emit-llvm -o - | %opt - %OPloadEnzyme %enzyme -S | %lli -
2+
// RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O1 %s -S -emit-llvm -o - | %opt - %OPloadEnzyme %enzyme -S | %lli -
3+
// RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O2 %s -S -emit-llvm -o - | %opt - %OPloadEnzyme %enzyme -S | %lli -
4+
// RUN: %clang++ -std=c++11 -fno-exceptions -ffast-math -O3 %s -S -emit-llvm -o - | %opt - %OPloadEnzyme %enzyme -S | %lli -
5+
6+
#include "../test_utils.h"
7+
8+
#include <cmath>
9+
#include <string>
10+
#include <unordered_map>
11+
12+
13+
extern double __enzyme_autodiff(void*, double);
14+
15+
std::unordered_map<std::string, double>
16+
fill_umap(double x, double y) {
17+
std::unordered_map<std::string, double> data;
18+
19+
data["alpha_x"] = x*x + y;
20+
data["alpha_y"] = y*y + 2.;
21+
22+
return data;
23+
}
24+
25+
double compute_umap_op(double r) {
26+
double x = std::sqrt(r);
27+
double y = std::log(r);
28+
29+
std::unordered_map<std::string, double> data = fill_umap(x, y);
30+
31+
// FIXME: memory leak
32+
// https://github.com/EnzymeAD/Enzyme/issues/2367#issuecomment-3025856672
33+
return data["alpha_x"];
34+
}
35+
36+
/* FIXME
37+
double compute_umap_at(double r) {
38+
double x = std::sqrt(r);
39+
double y = std::log(r);
40+
41+
std::unordered_map<std::string, double> const data = fill_umap(x, y);
42+
43+
// FIXME: fails to compile
44+
// https://fwd.gymni.ch/oGas9k
45+
return data.at("alpha_x");
46+
}
47+
*/
48+
49+
void test_umap() {
50+
double q1_k = -3.0;
51+
52+
// operator[]
53+
{
54+
// normal
55+
double const alpha_x = compute_umap_op(q1_k);
56+
APPROX_EQ( alpha_x, 4.098612, 1e-6);
57+
58+
// diff
59+
double ddx = __enzyme_autodiff((void*) compute_umap_op, q1_k);
60+
APPROX_EQ( ddx, 1.333333, 1e-6);
61+
}
62+
63+
// at()
64+
/* FIXME
65+
{
66+
// normal
67+
double const alpha_x = compute_umap_at(q1_k);
68+
APPROX_EQ( alpha_x, 4.098612, 1e-6);
69+
70+
// diff
71+
double ddx = __enzyme_autodiff((void*) compute_umap_at, q1_k);
72+
APPROX_EQ( ddx, 1.333333, 1e-6);
73+
}*/
74+
}
75+
76+
int main() {
77+
test_umap();
78+
79+
return 0;
80+
}
81+
82+

0 commit comments

Comments
 (0)