From 5299ffcef253cb1547cf1bdff83d6e5ef6b5ce21 Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Wed, 4 Sep 2019 18:17:07 -0400 Subject: [PATCH] [llvm] Use branch weight metadata in some cases. (mono/mono#16668) * [llvm] Add functions to create weighted branches. * [llvm] Use weighted branches in GC_SAFE_POINT, some llvm passes doesn't seem to recognize llvm.expect so the LLVM JIT generates the slow inline. Commit migrated from https://github.com/mono/mono/commit/db626fe9d370fb1f431667ae31a370b70275ef2b --- src/mono/mono/mini/mini-llvm-cpp.cpp | 21 +++++++++++++++++++++ src/mono/mono/mini/mini-llvm-cpp.h | 6 ++++++ src/mono/mono/mini/mini-llvm.c | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/mono/mono/mini/mini-llvm-cpp.cpp b/src/mono/mono/mini/mini-llvm-cpp.cpp index 451a27c..99d4487 100644 --- a/src/mono/mono/mini/mini-llvm-cpp.cpp +++ b/src/mono/mono/mini/mini-llvm-cpp.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include "mini-llvm-cpp.h" @@ -201,6 +202,26 @@ mono_llvm_build_fence (LLVMBuilderRef builder, BarrierKind kind) return wrap (ins); } +LLVMValueRef +mono_llvm_build_weighted_branch (LLVMBuilderRef builder, LLVMValueRef cond, LLVMBasicBlockRef t, LLVMBasicBlockRef f, uint32_t t_weight, uint32_t f_weight) +{ + auto b = unwrap (builder); + auto &ctx = b->getContext (); + MDBuilder mdb{ctx}; + auto weights = mdb.createBranchWeights (t_weight, f_weight); + auto ins = b->CreateCondBr (unwrap (cond), unwrap (t), unwrap (f), weights); + return wrap (ins); +} + +void +mono_llvm_set_implicit_branch (LLVMBuilderRef builder, LLVMValueRef branch) +{ + auto b = unwrap (builder); + auto &ctx = b->getContext (); + auto ins = unwrap (branch); + ins->setMetadata (LLVMContext::MD_make_implicit, MDNode::get (ctx, {})); +} + void mono_llvm_set_must_tailcall (LLVMValueRef call_ins) { diff --git a/src/mono/mono/mini/mini-llvm-cpp.h b/src/mono/mono/mini/mini-llvm-cpp.h index aeadc54..bc5cbf3 100644 --- a/src/mono/mono/mini/mini-llvm-cpp.h +++ b/src/mono/mono/mini/mini-llvm-cpp.h @@ -88,6 +88,12 @@ mono_llvm_replace_uses_of (LLVMValueRef var, LLVMValueRef v); LLVMValueRef mono_llvm_build_cmpxchg (LLVMBuilderRef builder, LLVMValueRef addr, LLVMValueRef comparand, LLVMValueRef value); +LLVMValueRef +mono_llvm_build_weighted_branch (LLVMBuilderRef builder, LLVMValueRef cond, LLVMBasicBlockRef t, LLVMBasicBlockRef f, uint32_t t_weight, uint32_t f_weight); + +void +mono_llvm_set_implicit_branch (LLVMBuilderRef builder, LLVMValueRef branch); + void mono_llvm_set_must_tailcall (LLVMValueRef call_ins); diff --git a/src/mono/mono/mini/mini-llvm.c b/src/mono/mono/mini/mini-llvm.c index 255c70d..b13c8b1 100644 --- a/src/mono/mono/mini/mini-llvm.c +++ b/src/mono/mono/mini/mini-llvm.c @@ -6435,7 +6435,7 @@ process_bb (EmitContext *ctx, MonoBasicBlock *bb) args [1] = LLVMConstInt (LLVMInt1Type (), 1, FALSE); cmp = LLVMBuildCall (ctx->builder, get_intrins (ctx, INTRINS_EXPECT_I1), args, 2, ""); - LLVMBuildCondBr (builder, cmp, cont_bb, poll_bb); + mono_llvm_build_weighted_branch (builder, cmp, cont_bb, poll_bb, 64, 4); ctx->builder = builder = create_builder (ctx); LLVMPositionBuilderAtEnd (builder, poll_bb); -- 2.7.4