Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1108)

Unified Diff: runtime/vm/intrinsifier.cc

Issue 1821783002: Add graph intrinsics for many math library functions (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/vm/intrinsifier.h ('k') | runtime/vm/intrinsifier_arm.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/intrinsifier.cc
diff --git a/runtime/vm/intrinsifier.cc b/runtime/vm/intrinsifier.cc
index af15e7a7f2a81df0e199b6cf7518a5bc31da34a0..364004a2a624bb6470065b374e9e5dd30df83ef2 100644
--- a/runtime/vm/intrinsifier.cc
+++ b/runtime/vm/intrinsifier.cc
@@ -21,16 +21,39 @@
namespace dart {
DEFINE_FLAG(bool, intrinsify, true, "Instrinsify when possible");
+DEFINE_FLAG(bool, trace_intrinsifier, false, "Trace intrinsifier");
DECLARE_FLAG(bool, code_comments);
DECLARE_FLAG(bool, print_flow_graph);
DECLARE_FLAG(bool, print_flow_graph_optimized);
bool Intrinsifier::CanIntrinsify(const Function& function) {
+ if (FLAG_trace_intrinsifier) {
+ THR_Print("CanIntrinsify %s ->", function.ToQualifiedCString());
+ }
if (!FLAG_intrinsify) return false;
- if (function.IsClosureFunction()) return false;
+ if (function.IsClosureFunction()) {
+ if (FLAG_trace_intrinsifier) {
+ THR_Print("No, closure function.\n");
+ }
+ return false;
+ }
// Can occur because of compile-all flag.
- if (function.is_external()) return false;
- return function.is_intrinsic();
+ if (function.is_external()) {
+ if (FLAG_trace_intrinsifier) {
+ THR_Print("No, external function.\n");
+ }
+ return false;
+ }
+ if (!function.is_intrinsic()) {
+ if (FLAG_trace_intrinsifier) {
+ THR_Print("No, not intrinsic function.\n");
+ }
+ return false;
+ }
+ if (FLAG_trace_intrinsifier) {
+ THR_Print("Yes.\n");
+ }
+ return true;
}
@@ -78,6 +101,7 @@ void Intrinsifier::InitializeState() {
lib = Library::MathLibrary();
ASSERT(!lib.IsNull());
MATH_LIB_INTRINSIC_LIST(SETUP_FUNCTION);
+ GRAPH_MATH_LIB_INTRINSIC_LIST(SETUP_FUNCTION);
// Set up all dart:typed_data lib functions that can be intrinsified.
lib = Library::TypedDataLibrary();
@@ -94,12 +118,11 @@ void Intrinsifier::InitializeState() {
}
#endif // defined(DART_NO_SNAPSHOT).
-
static void EmitCodeFor(FlowGraphCompiler* compiler,
FlowGraph* graph) {
// The FlowGraph here is constructed by the intrinsics builder methods, and
// is different from compiler->flow_graph(), the original method's flow graph.
- compiler->assembler()->Comment("Graph intrinsic");
+ compiler->assembler()->Comment("Graph intrinsic begin");
for (intptr_t i = 0; i < graph->reverse_postorder().length(); i++) {
BlockEntryInstr* block = graph->reverse_postorder()[i];
if (block->IsGraphEntry()) continue; // No code for graph entry needed.
@@ -115,6 +138,11 @@ static void EmitCodeFor(FlowGraphCompiler* compiler,
if (instr->IsParallelMove()) {
compiler->parallel_move_resolver()->EmitNativeCode(
instr->AsParallelMove());
+ } else if (instr->IsInvokeMathCFunction()) {
+ ASSERT(instr->locs() != NULL);
+ Intrinsifier::IntrinsicCallPrologue(compiler->assembler());
+ instr->EmitNativeCode(compiler);
+ Intrinsifier::IntrinsicCallEpilogue(compiler->assembler());
} else {
ASSERT(instr->locs() != NULL);
// Calls are not supported in intrinsics code.
@@ -123,6 +151,7 @@ static void EmitCodeFor(FlowGraphCompiler* compiler,
}
}
}
+ compiler->assembler()->Comment("Graph intrinsic end");
}
@@ -224,6 +253,11 @@ static intptr_t CidForRepresentation(Representation rep) {
}
+// Notes about the graph intrinsics:
+//
+// IR instructions which would jump to a deoptimization sequence on failure
+// instead branch to the intrinsic slow path.
+//
class BlockBuilder : public ValueObject {
public:
BlockBuilder(FlowGraph* flow_graph, TargetEntryInstr* entry)
@@ -269,17 +303,45 @@ class BlockBuilder : public ValueObject {
new ConstantInstr(Object::ZoneHandle(Object::null())));
}
- Definition* AddUnboxInstr(Representation rep, Value* value) {
+ Definition* AddUnboxInstr(Representation rep,
+ Value* value,
+ bool is_checked) {
Definition* unboxed_value = AddDefinition(
UnboxInstr::Create(rep, value, Thread::kNoDeoptId));
- // Manually adjust reaching type because there is no type propagation
- // when building intrinsics.
- unboxed_value->AsUnbox()->value()->SetReachingType(ZoneCompileType::Wrap(
- CompileType::FromCid(CidForRepresentation(rep))));
+ if (is_checked) {
+ // The type of |value| has already been checked and it is safe to
+ // adjust reaching type. This is done manually because there is no type
+ // propagation when building intrinsics.
+ unboxed_value->AsUnbox()->value()->SetReachingType(ZoneCompileType::Wrap(
+ CompileType::FromCid(CidForRepresentation(rep))));
+ }
return unboxed_value;
}
+ Definition* AddUnboxInstr(Representation rep,
+ Definition* boxed,
+ bool is_checked) {
+ return AddUnboxInstr(rep, new Value(boxed), is_checked);
+ }
+
+ Definition* InvokeMathCFunction(MethodRecognizer::Kind recognized_kind,
+ ZoneGrowableArray<Value*>* args) {
+ return InvokeMathCFunctionHelper(recognized_kind, args);
+ }
+
private:
+ Definition* InvokeMathCFunctionHelper(MethodRecognizer::Kind recognized_kind,
+ ZoneGrowableArray<Value*>* args) {
+ InvokeMathCFunctionInstr* invoke_math_c_function =
+ new InvokeMathCFunctionInstr(args,
+ Thread::kNoDeoptId,
+ recognized_kind,
+ TokenPos());
+ AddDefinition(invoke_math_c_function);
+ return invoke_math_c_function;
+ }
+
+
FlowGraph* flow_graph_;
BlockEntryInstr* entry_;
Instruction* current_;
@@ -460,7 +522,9 @@ bool Intrinsifier::Build_Uint32ArraySetIndexed(FlowGraph* flow_graph) {
PrepareIndexedOp(&builder, array, index, TypedData::length_offset());
Definition* unboxed_value =
- builder.AddUnboxInstr(kUnboxedUint32, new Value(value));
+ builder.AddUnboxInstr(kUnboxedUint32,
+ new Value(value),
+ /* is_checked = */ true);
builder.AddInstruction(
new StoreIndexedInstr(new Value(array),
@@ -528,7 +592,9 @@ bool Intrinsifier::Build_Float64ArraySetIndexed(FlowGraph* flow_graph) {
value_check,
builder.TokenPos()));
Definition* double_value =
- builder.AddUnboxInstr(kUnboxedDouble, new Value(value));
+ builder.AddUnboxInstr(kUnboxedDouble,
+ new Value(value),
+ /* is_checked = */ true);
builder.AddInstruction(
new StoreIndexedInstr(new Value(array),
@@ -596,10 +662,14 @@ static bool BuildBinaryFloat32x4Op(FlowGraph* flow_graph, Token::Kind kind) {
value_check,
builder.TokenPos()));
Definition* left_simd =
- builder.AddUnboxInstr(kUnboxedFloat32x4, new Value(left));
+ builder.AddUnboxInstr(kUnboxedFloat32x4,
+ new Value(left),
+ /* is_checked = */ true);
Definition* right_simd =
- builder.AddUnboxInstr(kUnboxedFloat32x4, new Value(right));
+ builder.AddUnboxInstr(kUnboxedFloat32x4,
+ new Value(right),
+ /* is_checked = */ true);
Definition* unboxed_result = builder.AddDefinition(
new BinaryFloat32x4OpInstr(kind,
@@ -638,7 +708,9 @@ static bool BuildFloat32x4Shuffle(FlowGraph* flow_graph,
Definition* receiver = builder.AddParameter(1);
Definition* unboxed_receiver =
- builder.AddUnboxInstr(kUnboxedFloat32x4, new Value(receiver));
+ builder.AddUnboxInstr(kUnboxedFloat32x4,
+ new Value(receiver),
+ /* is_checked = */ true);
Definition* unboxed_result = builder.AddDefinition(
new Simd32x4ShuffleInstr(kind,
@@ -872,7 +944,9 @@ bool Intrinsifier::Build_DoubleFlipSignBit(FlowGraph* flow_graph) {
Definition* receiver = builder.AddParameter(1);
Definition* unboxed_value =
- builder.AddUnboxInstr(kUnboxedDouble, new Value(receiver));
+ builder.AddUnboxInstr(kUnboxedDouble,
+ new Value(receiver),
+ /* is_checked = */ true);
Definition* unboxed_result = builder.AddDefinition(
new UnaryDoubleOpInstr(Token::kNEGATE,
new Value(unboxed_value),
@@ -883,4 +957,115 @@ bool Intrinsifier::Build_DoubleFlipSignBit(FlowGraph* flow_graph) {
return true;
}
+
+static bool BuildInvokeMathCFunction(BlockBuilder* builder,
+ MethodRecognizer::Kind kind,
+ intptr_t num_parameters = 1) {
+ ZoneGrowableArray<Value*>* args =
+ new ZoneGrowableArray<Value*>(num_parameters);
+
+ for (intptr_t i = 0; i < num_parameters; i++) {
+ const intptr_t parameter_index = (num_parameters - i);
+ Definition* value = builder->AddParameter(parameter_index);
+ Definition* unboxed_value =
+ builder->AddUnboxInstr(kUnboxedDouble, value, /* is_checked = */ false);
+ args->Add(new Value(unboxed_value));
+ }
+
+ Definition* unboxed_result =
+ builder->InvokeMathCFunction(kind, args);
+
+ Definition* result = builder->AddDefinition(
+ BoxInstr::Create(kUnboxedDouble, new Value(unboxed_result)));
+
+ builder->AddIntrinsicReturn(new Value(result));
+
+ return true;
+}
+
+
+bool Intrinsifier::Build_MathSin(FlowGraph* flow_graph) {
+ if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
+
+ GraphEntryInstr* graph_entry = flow_graph->graph_entry();
+ TargetEntryInstr* normal_entry = graph_entry->normal_entry();
+ BlockBuilder builder(flow_graph, normal_entry);
+
+ return BuildInvokeMathCFunction(&builder,
+ MethodRecognizer::kMathSin);
+}
+
+
+bool Intrinsifier::Build_MathCos(FlowGraph* flow_graph) {
+ if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
+
+ GraphEntryInstr* graph_entry = flow_graph->graph_entry();
+ TargetEntryInstr* normal_entry = graph_entry->normal_entry();
+ BlockBuilder builder(flow_graph, normal_entry);
+
+ return BuildInvokeMathCFunction(&builder,
+ MethodRecognizer::kMathCos);
+}
+
+
+bool Intrinsifier::Build_MathTan(FlowGraph* flow_graph) {
+ if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
+
+ GraphEntryInstr* graph_entry = flow_graph->graph_entry();
+ TargetEntryInstr* normal_entry = graph_entry->normal_entry();
+ BlockBuilder builder(flow_graph, normal_entry);
+
+ return BuildInvokeMathCFunction(&builder,
+ MethodRecognizer::kMathTan);
+}
+
+
+bool Intrinsifier::Build_MathAsin(FlowGraph* flow_graph) {
+ if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
+
+ GraphEntryInstr* graph_entry = flow_graph->graph_entry();
+ TargetEntryInstr* normal_entry = graph_entry->normal_entry();
+ BlockBuilder builder(flow_graph, normal_entry);
+
+ return BuildInvokeMathCFunction(&builder,
+ MethodRecognizer::kMathAsin);
+}
+
+
+bool Intrinsifier::Build_MathAcos(FlowGraph* flow_graph) {
+ if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
+
+ GraphEntryInstr* graph_entry = flow_graph->graph_entry();
+ TargetEntryInstr* normal_entry = graph_entry->normal_entry();
+ BlockBuilder builder(flow_graph, normal_entry);
+
+ return BuildInvokeMathCFunction(&builder,
+ MethodRecognizer::kMathAcos);
+}
+
+
+bool Intrinsifier::Build_MathAtan(FlowGraph* flow_graph) {
+ if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
+
+ GraphEntryInstr* graph_entry = flow_graph->graph_entry();
+ TargetEntryInstr* normal_entry = graph_entry->normal_entry();
+ BlockBuilder builder(flow_graph, normal_entry);
+
+ return BuildInvokeMathCFunction(&builder,
+ MethodRecognizer::kMathAtan);
+}
+
+
+bool Intrinsifier::Build_MathAtan2(FlowGraph* flow_graph) {
+ if (!FlowGraphCompiler::SupportsUnboxedDoubles()) return false;
+
+ GraphEntryInstr* graph_entry = flow_graph->graph_entry();
+ TargetEntryInstr* normal_entry = graph_entry->normal_entry();
+ BlockBuilder builder(flow_graph, normal_entry);
+
+ return BuildInvokeMathCFunction(&builder,
+ MethodRecognizer::kMathAtan2,
+ /* num_parameters = */ 2);
+}
+
} // namespace dart
« no previous file with comments | « runtime/vm/intrinsifier.h ('k') | runtime/vm/intrinsifier_arm.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698