File: create_llvm_prof.cc

package info (click to toggle)
autofdo 0.18-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 92,568 kB
  • sloc: cpp: 26,830; sh: 12,538; makefile: 344; ansic: 134; python: 95
file content (95 lines) | stat: -rw-r--r-- 3,485 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This program creates an LLVM profile from an AutoFDO source.

#include "config.h"

#if defined(HAVE_LLVM)
#include <memory>

#include "gflags/gflags.h"
#include "profile_creator.h"
#include "llvm_profile_writer.h"

DEFINE_string(profile, "perf.data", "Input profile file name");
DEFINE_string(profiler, "perf", "Input profile type");
DEFINE_string(out, "", "Output profile file name");
DEFINE_string(gcov, "",
              "Output profile file name. Alias for --out; used for "
              "flag compatibility with create_gcov");
DEFINE_string(binary, "a.out", "Binary file name");
DEFINE_string(format, "binary",
              "LLVM profile format to emit. Possible values are 'text' or "
              "'binary'. The binary format is a more compact representation, "
              "but the text format is human readable and more likely to be "
              "compatible with older versions of LLVM.");

#define PROG_USAGE                                           \
  "\nConverts a sample profile collected with Perf "         \
  "(https://perf.wiki.kernel.org/)\n"                        \
  "into an LLVM profile. The output file can be used with\n" \
  "Clang's -fprofile-sample-use flag."

int main(int argc, char **argv) {
  google::SetUsageMessage(PROG_USAGE);
  google::ParseCommandLineFlags(&argc, &argv, true);
  google::InitGoogleLogging(argv[0]);

  // If the user specified --gcov instead of --out, use that value.
  // If both are used, they must match.
  if (!FLAGS_gcov.empty()) {
    if (!FLAGS_out.empty() && FLAGS_out != FLAGS_gcov) {
      LOG(ERROR) << "--out and --gcov specified with different values.";
      LOG(ERROR) << "Please use only one of the two flags.";
      return 1;
    }
    FLAGS_out = FLAGS_gcov;
  }

  if (FLAGS_out.empty()) {
    LOG(ERROR) << "Need a name for the generated LLVM profile file.";
    LOG(ERROR) << "Use --gcov or --out to specify an output file.";
    return 1;
  }

  std::unique_ptr<autofdo::LLVMProfileWriter> writer(nullptr);
  if (FLAGS_format == "text") {
    writer.reset(new autofdo::LLVMProfileWriter(llvm::sampleprof::SPF_Text));
  } else if (FLAGS_format == "binary") {
    writer.reset(new autofdo::LLVMProfileWriter(llvm::sampleprof::SPF_Binary));
  } else {
    LOG(ERROR) << "--format must be one of 'text' or 'binary'";
    return 1;
  }

  autofdo::ProfileCreator creator(FLAGS_binary);
  creator.set_use_discriminator_encoding(true);
  if (creator.CreateProfile(FLAGS_profile, FLAGS_profiler, writer.get(),
                            FLAGS_out))
    return 0;
  else
    return -1;
}

#else
#include <stdio.h>
int main(int argc, char **argv) {
  fprintf(stderr,
          "ERROR: LLVM support was not enabled in this configuration.\nPlease "
          "configure and rebuild with:\n\n$ ./configure "
          "--with-llvm=<path-to-llvm-config>\n\n");
  return -1;
}
#endif  // HAVE_LLVM