Skip to content

Commit

Permalink
Merged master:c6f51377e12b into amd-gfx:4a16eb27d030
Browse files Browse the repository at this point in the history
Local branch amd-gfx 4a16eb2 Merged master:06d100a69a08 into amd-gfx:9bd98790e775
Remote branch master c6f5137 [libcxx/variant] Add a few benchmarks for .
  • Loading branch information
Sw authored and Sw committed Aug 11, 2020
2 parents 4a16eb2 + c6f5137 commit b49b4e7
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 84 deletions.
58 changes: 58 additions & 0 deletions libcxx/benchmarks/VariantBenchmarks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef BENCHMARK_VARIANT_BENCHMARKS_H
#define BENCHMARK_VARIANT_BENCHMARKS_H

#include <array>
#include <cstddef>
#include <tuple>
#include <type_traits>
#include <variant>

#include "benchmark/benchmark.h"

#include "GenerateInput.h"

namespace VariantBenchmarks {

template <std::size_t I>
struct S {
static constexpr size_t v = I;
};

template <std::size_t N, std::size_t... Is>
static auto genVariants(std::index_sequence<Is...>) {
using V = std::variant<S<Is>...>;
using F = V (*)();
static constexpr F fs[] = {[] { return V(std::in_place_index<Is>); }...};

std::array<V, N> result = {};
for (auto& v : result) {
v = fs[getRandomInteger(0ul, sizeof...(Is) - 1)]();
}

return result;
}

template <std::size_t N, std::size_t Alts>
static void BM_Visit(benchmark::State& state) {
auto args = genVariants<N>(std::make_index_sequence<Alts>{});
for (auto _ : state) {
benchmark::DoNotOptimize(std::apply(
[](auto... vs) {
return std::visit([](auto... is) { return (is.v + ... + 0); }, vs...);
},
args));
}
}

} // end namespace VariantBenchmarks

#endif // BENCHMARK_VARIANT_BENCHMARKS_H
27 changes: 27 additions & 0 deletions libcxx/benchmarks/variant_visit_1.bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "benchmark/benchmark.h"

#include "VariantBenchmarks.h"

using namespace VariantBenchmarks;

BENCHMARK_TEMPLATE(BM_Visit, 1, 1);
BENCHMARK_TEMPLATE(BM_Visit, 1, 2);
BENCHMARK_TEMPLATE(BM_Visit, 1, 3);
BENCHMARK_TEMPLATE(BM_Visit, 1, 4);
BENCHMARK_TEMPLATE(BM_Visit, 1, 5);
BENCHMARK_TEMPLATE(BM_Visit, 1, 6);
BENCHMARK_TEMPLATE(BM_Visit, 1, 7);
BENCHMARK_TEMPLATE(BM_Visit, 1, 8);
BENCHMARK_TEMPLATE(BM_Visit, 1, 9);
BENCHMARK_TEMPLATE(BM_Visit, 1, 10);
BENCHMARK_TEMPLATE(BM_Visit, 1, 20);
BENCHMARK_TEMPLATE(BM_Visit, 1, 30);
BENCHMARK_TEMPLATE(BM_Visit, 1, 40);
BENCHMARK_TEMPLATE(BM_Visit, 1, 50);
BENCHMARK_TEMPLATE(BM_Visit, 1, 60);
BENCHMARK_TEMPLATE(BM_Visit, 1, 70);
BENCHMARK_TEMPLATE(BM_Visit, 1, 80);
BENCHMARK_TEMPLATE(BM_Visit, 1, 90);
BENCHMARK_TEMPLATE(BM_Visit, 1, 100);

BENCHMARK_MAIN();
22 changes: 22 additions & 0 deletions libcxx/benchmarks/variant_visit_2.bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "benchmark/benchmark.h"

#include "VariantBenchmarks.h"

using namespace VariantBenchmarks;

BENCHMARK_TEMPLATE(BM_Visit, 2, 1);
BENCHMARK_TEMPLATE(BM_Visit, 2, 2);
BENCHMARK_TEMPLATE(BM_Visit, 2, 3);
BENCHMARK_TEMPLATE(BM_Visit, 2, 4);
BENCHMARK_TEMPLATE(BM_Visit, 2, 5);
BENCHMARK_TEMPLATE(BM_Visit, 2, 6);
BENCHMARK_TEMPLATE(BM_Visit, 2, 7);
BENCHMARK_TEMPLATE(BM_Visit, 2, 8);
BENCHMARK_TEMPLATE(BM_Visit, 2, 9);
BENCHMARK_TEMPLATE(BM_Visit, 2, 10);
BENCHMARK_TEMPLATE(BM_Visit, 2, 20);
BENCHMARK_TEMPLATE(BM_Visit, 2, 30);
BENCHMARK_TEMPLATE(BM_Visit, 2, 40);
BENCHMARK_TEMPLATE(BM_Visit, 2, 50);

BENCHMARK_MAIN();
20 changes: 20 additions & 0 deletions libcxx/benchmarks/variant_visit_3.bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "benchmark/benchmark.h"

#include "VariantBenchmarks.h"

using namespace VariantBenchmarks;

BENCHMARK_TEMPLATE(BM_Visit, 3, 1);
BENCHMARK_TEMPLATE(BM_Visit, 3, 2);
BENCHMARK_TEMPLATE(BM_Visit, 3, 3);
BENCHMARK_TEMPLATE(BM_Visit, 3, 4);
BENCHMARK_TEMPLATE(BM_Visit, 3, 5);
BENCHMARK_TEMPLATE(BM_Visit, 3, 6);
BENCHMARK_TEMPLATE(BM_Visit, 3, 7);
BENCHMARK_TEMPLATE(BM_Visit, 3, 8);
BENCHMARK_TEMPLATE(BM_Visit, 3, 9);
BENCHMARK_TEMPLATE(BM_Visit, 3, 10);
BENCHMARK_TEMPLATE(BM_Visit, 3, 15);
BENCHMARK_TEMPLATE(BM_Visit, 3, 20);

BENCHMARK_MAIN();
4 changes: 0 additions & 4 deletions llvm/include/llvm/IR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,6 @@ class Instruction : public User,
/// merged DebugLoc.
void applyMergedLocation(const DILocation *LocA, const DILocation *LocB);

/// Updates the debug location given that the instruction has been hoisted
/// from a block to a predecessor of that block.
void updateLocationAfterHoist();

private:
/// Return true if we have an entry in the on-the-side metadata hash.
bool hasMetadataHashEntry() const {
Expand Down
18 changes: 0 additions & 18 deletions llvm/lib/IR/DebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,24 +696,6 @@ void Instruction::applyMergedLocation(const DILocation *LocA,
setDebugLoc(DILocation::getMergedLocation(LocA, LocB));
}

void Instruction::updateLocationAfterHoist() {
const DebugLoc &DL = getDebugLoc();
if (!DL)
return;

// If we didn't hoist a call, drop the location to allow a location from a
// preceding instruction to propagate.
if (!isa<CallBase>(this)) {
setDebugLoc(DebugLoc());
return;
}

// Set a line 0 location for (potentially inlinable) calls. Set the scope to
// the parent function's scope if it's available, and drop any inlinedAt info
// to avoid making it look like the inlined callee was reached early.
setDebugLoc(DebugLoc::get(0, 0, DL.getScope()));
}

//===----------------------------------------------------------------------===//
// LLVM C API implementations.
//===----------------------------------------------------------------------===//
Expand Down
14 changes: 6 additions & 8 deletions llvm/lib/Transforms/IPO/GlobalOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,19 +468,16 @@ static bool CanDoGlobalSRA(GlobalVariable *GV) {
/// Copy over the debug info for a variable to its SRA replacements.
static void transferSRADebugInfo(GlobalVariable *GV, GlobalVariable *NGV,
uint64_t FragmentOffsetInBits,
uint64_t FragmentSizeInBits) {
uint64_t FragmentSizeInBits,
uint64_t VarSize) {
SmallVector<DIGlobalVariableExpression *, 1> GVs;
GV->getDebugInfo(GVs);
for (auto *GVE : GVs) {
DIVariable *Var = GVE->getVariable();
Optional<uint64_t> VarSize = Var->getSizeInBits();

DIExpression *Expr = GVE->getExpression();
// If the FragmentSize is smaller than the variable,
// emit a fragment expression.
// If the variable size is unknown a fragment must be
// emitted to be safe.
if (!VarSize || FragmentSizeInBits < *VarSize) {
if (FragmentSizeInBits < VarSize) {
if (auto E = DIExpression::createFragmentExpression(
Expr, FragmentOffsetInBits, FragmentSizeInBits))
Expr = *E;
Expand All @@ -505,6 +502,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
assert(GV->hasLocalLinkage());
Constant *Init = GV->getInitializer();
Type *Ty = Init->getType();
uint64_t VarSize = DL.getTypeSizeInBits(Ty);

std::map<unsigned, GlobalVariable *> NewGlobals;

Expand Down Expand Up @@ -560,7 +558,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
// Copy over the debug info for the variable.
uint64_t Size = DL.getTypeAllocSizeInBits(NGV->getValueType());
uint64_t FragmentOffsetInBits = Layout.getElementOffsetInBits(ElementIdx);
transferSRADebugInfo(GV, NGV, FragmentOffsetInBits, Size);
transferSRADebugInfo(GV, NGV, FragmentOffsetInBits, Size, VarSize);
} else {
uint64_t EltSize = DL.getTypeAllocSize(ElTy);
Align EltAlign = DL.getABITypeAlign(ElTy);
Expand All @@ -573,7 +571,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
if (NewAlign > EltAlign)
NGV->setAlignment(NewAlign);
transferSRADebugInfo(GV, NGV, FragmentSizeInBits * ElementIdx,
FragmentSizeInBits);
FragmentSizeInBits, VarSize);
}
}

Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/Transforms/Scalar/GVN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
Expand Down Expand Up @@ -1313,7 +1314,8 @@ bool GVN::PerformLoadPRE(LoadInst *LI, AvailValInBlkVect &ValuesPerBlock,
// Instructions that have been inserted in predecessor(s) to materialize
// the load address do not retain their original debug locations. Doing
// so could lead to confusing (but correct) source attributions.
I->updateLocationAfterHoist();
if (const DebugLoc &DL = I->getDebugLoc())
I->setDebugLoc(DebugLoc::get(0, 0, DL.getScope(), DL.getInlinedAt()));

// FIXME: We really _ought_ to insert these value numbers into their
// parent's availability map. However, in doing so, we risk getting into
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Transforms/Scalar/LICM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,10 @@ static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
// Move the new node to the destination block, before its terminator.
moveInstructionBefore(I, *Dest->getTerminator(), *SafetyInfo, MSSAU, SE);

I.updateLocationAfterHoist();
// Apply line 0 debug locations when we are moving instructions to different
// basic blocks because we want to avoid jumpy line tables.
if (const DebugLoc &DL = I.getDebugLoc())
I.setDebugLoc(DebugLoc::get(0, 0, DL.getScope(), DL.getInlinedAt()));

if (isa<LoadInst>(I))
++NumMovedLoads;
Expand Down
63 changes: 63 additions & 0 deletions llvm/test/DebugInfo/Generic/global-sra-struct-fwd-decl.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
; RUN: opt -S -globalopt < %s | FileCheck %s
; Generated at -O2 -g from:
; typedef struct {} a;
; static struct {
; long b;
; a c;
; } d;
; e() {
; long f = d.b + 1;
; d.b = f;
; }
; (IR is modified so that d's struct type is forward declared.)

; Check that the global variable "d" is not
; emitted as a fragment if its struct type is
; forward declared but d.c has zero length, so
; a fragment shouldn't be emitted.

source_filename = "t.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.anon = type { i64, %struct.a }
%struct.a = type {}

; CHECK: @d.0 = internal unnamed_addr global i64 0, align 8, !dbg ![[GVE:.*]]
@d = internal global %struct.anon zeroinitializer, align 8, !dbg !0

; Function Attrs: noinline nounwind uwtable
define dso_local i32 @e() #0 !dbg !18 {
entry:
%0 = load i64, i64* getelementptr inbounds (%struct.anon, %struct.anon* @d, i32 0, i32 0), align 8
%add = add nsw i64 %0, 1
call void @llvm.dbg.value(metadata i64 %add, metadata !24, metadata !DIExpression()), !dbg !25
store i64 %add, i64* getelementptr inbounds (%struct.anon, %struct.anon* @d, i32 0, i32 0), align 8
ret i32 undef
}

; Function Attrs: nounwind readnone speculatable willreturn
declare void @llvm.dbg.declare(metadata, metadata, metadata)

; Function Attrs: nounwind readnone speculatable willreturn
declare void @llvm.dbg.value(metadata, metadata, metadata)

!llvm.dbg.cu = !{!2}
!llvm.module.flags = !{!14, !15}

; CHECK: ![[GVE]] = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "d", scope: !2, file: !3, line: 6, type: !7, isLocal: true, isDefinition: true)
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !{}, globals: !{!0}, splitDebugInlining: false, nameTableKind: None)
!3 = !DIFile(filename: "t.c", directory: "/")
!7 = distinct !DICompositeType(tag: DW_TAG_structure_type, file: !3, line: 3, flags: DIFlagFwdDecl)
!10 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed)
!12 = !DIDerivedType(tag: DW_TAG_typedef, name: "a", file: !3, line: 2, baseType: !13)
!13 = distinct !DICompositeType(tag: DW_TAG_structure_type, file: !3, line: 1, elements: !{})
!14 = !{i32 7, !"Dwarf Version", i32 4}
!15 = !{i32 2, !"Debug Info Version", i32 3}
!18 = distinct !DISubprogram(name: "e", scope: !3, file: !3, line: 7, type: !19, scopeLine: 7, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !{})
!19 = !DISubroutineType(types: !{!21})
!21 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!24 = !DILocalVariable(name: "f", scope: !18, file: !3, line: 8, type: !10)
!25 = !DILocation(line: 0, scope: !18)
3 changes: 2 additions & 1 deletion llvm/test/DebugInfo/Generic/licm-hoist-debug-loc.ll
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
; We make sure that the instruction that is hoisted into the preheader
; does not have a debug location.
; CHECK: for.body.lr.ph:
; CHECK: getelementptr{{.*}}%p.addr, i64 4{{$}}
; CHECK: getelementptr{{.*}}%p.addr, i64 4{{.*}} !dbg [[zero:![0-9]+]]
; CHECK: for.body:
; CHECK: [[zero]] = !DILocation(line: 0
;
; ModuleID = 't.ll'
source_filename = "test.c"
Expand Down
7 changes: 4 additions & 3 deletions llvm/test/Transforms/GVN/PRE/phi-translate.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ target datalayout = "e-p:64:64:64"

; CHECK-LABEL: @foo(
; CHECK: entry.end_crit_edge:
; CHECK: %[[INDEX:[a-z0-9.]+]] = sext i32 %x to i64{{$}}
; CHECK: %[[ADDRESS:[a-z0-9.]+]] = getelementptr [100 x i32], [100 x i32]* @G, i64 0, i64 %[[INDEX]]{{$}}
; CHECK: %[[INDEX:[a-z0-9.]+]] = sext i32 %x to i64{{.*}} !dbg [[ZERO_LOC:![0-9]+]]
; CHECK: %[[ADDRESS:[a-z0-9.]+]] = getelementptr [100 x i32], [100 x i32]* @G, i64 0, i64 %[[INDEX]]{{.*}} !dbg [[ZERO_LOC]]
; CHECK: %n.pre = load i32, i32* %[[ADDRESS]], align 4, !dbg [[N_LOC:![0-9]+]]
; CHECK: br label %end
; CHECK: then:
Expand All @@ -14,7 +14,8 @@ target datalayout = "e-p:64:64:64"
; CHECK: %n = phi i32 [ %n.pre, %entry.end_crit_edge ], [ %z, %then ], !dbg [[N_LOC]]
; CHECK: ret i32 %n

; CHECK: [[N_LOC]] = !DILocation(line: 47, column: 1, scope: !{{.*}})
; CHECK-DAG: [[N_LOC]] = !DILocation(line: 47, column: 1, scope: !{{.*}})
; CHECK-DAG: [[ZERO_LOC]] = !DILocation(line: 0

@G = external global [100 x i32]
define i32 @foo(i32 %x, i32 %z) !dbg !6 {
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/LICM/hoisting-preheader-debugloc.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; RUN: opt -S -licm < %s | FileCheck %s

; CHECK: %arrayidx4.promoted = load i32, i32* %arrayidx4, align 1, !tbaa !{{[0-9]+$}}
; CHECK: %arrayidx4.promoted = load i32, i32* %arrayidx4, align 1, !tbaa !59

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
Expand Down
Loading

0 comments on commit b49b4e7

Please sign in to comment.