blob: 6c728dd124d200107df1a60b62ad39961a51399a (
plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
/*===- PGOProfiling.c - Support library for PGO instrumentation -----------===*\
|*
|* The LLVM Compiler Infrastructure
|*
|* This file is distributed under the University of Illinois Open Source
|* License. See LICENSE.TXT for details.
|*
\*===----------------------------------------------------------------------===*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef _MSC_VER
#include <stdint.h>
#else
typedef unsigned int uint32_t;
typedef unsigned int uint64_t;
#endif
static FILE *OutputFile = NULL;
/*
* A list of functions to write out the data.
*/
typedef void (*writeout_fn)();
struct writeout_fn_node {
writeout_fn fn;
struct writeout_fn_node *next;
};
static struct writeout_fn_node *writeout_fn_head = NULL;
static struct writeout_fn_node *writeout_fn_tail = NULL;
void llvm_pgo_emit(const char *MangledName, uint32_t NumCounters,
uint64_t *Counters) {
uint32_t i;
fprintf(OutputFile, "%s %u\n", MangledName, NumCounters);
for (i = 0; i < NumCounters; ++i)
fprintf(OutputFile, "%" PRIu64 "\n", Counters[i]);
fprintf(OutputFile, "\n");
}
void llvm_pgo_register_writeout_function(writeout_fn fn) {
struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
new_node->fn = fn;
new_node->next = NULL;
if (!writeout_fn_head) {
writeout_fn_head = writeout_fn_tail = new_node;
} else {
writeout_fn_tail->next = new_node;
writeout_fn_tail = new_node;
}
}
void llvm_pgo_writeout_files() {
OutputFile = fopen("pgo-data", "w");
if (!OutputFile) return;
while (writeout_fn_head) {
struct writeout_fn_node *node = writeout_fn_head;
writeout_fn_head = writeout_fn_head->next;
node->fn();
free(node);
}
fclose(OutputFile);
}
void llvm_pgo_init(writeout_fn wfn) {
static int atexit_ran = 0;
if (wfn)
llvm_pgo_register_writeout_function(wfn);
if (atexit_ran == 0) {
atexit_ran = 1;
atexit(llvm_pgo_writeout_files);
}
}
|