summaryrefslogtreecommitdiff
path: root/lib/profile/InstrProfilingExtras.c
blob: 45a7f8ce52c97e795ce24596cbc5bf7ea7aa6bd1 (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
/*===- InstrProfilingExtras.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 "InstrProfiling.h"

void __llvm_pgo_write_file(const char *OutputName) {
  /* TODO: Requires libc: move to separate translation unit. */
  FILE *OutputFile;
  if (!OutputName || !OutputName[0])
    return;
  OutputFile = fopen(OutputName, "w");
  if (!OutputFile) return;

  /* TODO: mmap file to buffer of size __llvm_pgo_get_size_for_buffer() and
   * pass the buffer in, instead of the file.
   */
  __llvm_pgo_write_buffer(OutputFile);

  fclose(OutputFile);
}

void __llvm_pgo_write_default_file() {
  /* TODO: Requires libc: move to separate translation unit. */
  const char *OutputName = getenv("LLVM_PROFILE_FILE");
  if (OutputName == NULL || OutputName[0] == '\0')
    OutputName = "default.profdata";
  __llvm_pgo_write_file(OutputName);
}

void __llvm_pgo_register_write_atexit() {
  /* TODO: Requires libc: move to separate translation unit. */
  static int HasBeenRegistered = 0;

  if (!HasBeenRegistered) {
    HasBeenRegistered = 1;
    atexit(__llvm_pgo_write_default_file);
  }
}