Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8252505: C1/C2 compiler support for blackholes
Reviewed-by: vlivanov, aph
  • Loading branch information
shipilev committed Dec 6, 2020
1 parent 972bc3b commit e590618
Show file tree
Hide file tree
Showing 30 changed files with 1,520 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/hotspot/share/c1/c1_Compiler.cpp
Expand Up @@ -230,6 +230,8 @@ bool Compiler::is_intrinsic_supported(const methodHandle& method) {
break;
case vmIntrinsics::_getObjectSize:
break;
case vmIntrinsics::_blackhole:
break;
default:
return false; // Intrinsics not on the previous list are not available.
}
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/c1/c1_GraphBuilder.cpp
Expand Up @@ -3415,7 +3415,7 @@ bool GraphBuilder::try_inline(ciMethod* callee, bool holder_known, bool ignore_r

// handle intrinsics
if (callee->intrinsic_id() != vmIntrinsics::_none &&
(CheckIntrinsics ? callee->intrinsic_candidate() : true)) {
callee->check_intrinsic_candidate()) {
if (try_inline_intrinsics(callee, ignore_return)) {
print_inlining(callee, "intrinsic");
if (callee->has_reserved_stack_access()) {
Expand Down
21 changes: 21 additions & 0 deletions src/hotspot/share/c1/c1_LIRGenerator.cpp
Expand Up @@ -3206,6 +3206,10 @@ void LIRGenerator::do_Intrinsic(Intrinsic* x) {
do_vectorizedMismatch(x);
break;

case vmIntrinsics::_blackhole:
do_blackhole(x);
break;

default: ShouldNotReachHere(); break;
}
}
Expand Down Expand Up @@ -3625,6 +3629,23 @@ void LIRGenerator::do_RangeCheckPredicate(RangeCheckPredicate *x) {
}
}

void LIRGenerator::do_blackhole(Intrinsic *x) {
// If we have a receiver, then null-check and handle it separately
bool handle_receiver = x->needs_null_check();
if (handle_receiver) {
CodeEmitInfo* info = state_for(x);
LIRItem vitem(x->receiver(), this);
vitem.load_item();
__ null_check(vitem.result(), info);
}

for (int c = (handle_receiver ? 1 : 0); c < x->number_of_arguments(); c++) {
// Load the argument
LIRItem vitem(x->argument_at(c), this);
vitem.load_item();
// ...and leave it unused.
}
}

LIR_Opr LIRGenerator::call_runtime(Value arg1, address entry, ValueType* result_type, CodeEmitInfo* info) {
LIRItemList args(1);
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/c1/c1_LIRGenerator.hpp
Expand Up @@ -265,6 +265,7 @@ class LIRGenerator: public InstructionVisitor, public BlockClosure {
void do_update_CRC32(Intrinsic* x);
void do_update_CRC32C(Intrinsic* x);
void do_vectorizedMismatch(Intrinsic* x);
void do_blackhole(Intrinsic* x);

public:
LIR_Opr call_runtime(BasicTypeArray* signature, LIRItemList* args, address entry, ValueType* result_type, CodeEmitInfo* info);
Expand Down
4 changes: 4 additions & 0 deletions src/hotspot/share/ci/ciMethod.cpp
Expand Up @@ -155,6 +155,10 @@ ciMethod::ciMethod(const methodHandle& h_m, ciInstanceKlass* holder) :
ciReplay::initialize(this);
}
#endif

if (CompilerOracle::should_blackhole(h_m)) {
h_m->set_intrinsic_id(vmIntrinsics::_blackhole);
}
}


Expand Down
9 changes: 9 additions & 0 deletions src/hotspot/share/ci/ciMethod.hpp
Expand Up @@ -201,6 +201,15 @@ class ciMethod : public ciMetadata {
bool intrinsic_candidate() const { return get_Method()->intrinsic_candidate(); }
bool is_static_initializer() const { return get_Method()->is_static_initializer(); }

bool check_intrinsic_candidate() const {
if (intrinsic_id() == vmIntrinsics::_blackhole) {
// This is the intrinsic without an associated method, so no intrinsic_candidate
// flag is set. The intrinsic is still correct.
return true;
}
return (CheckIntrinsics ? intrinsic_candidate() : true);
}

int highest_osr_comp_level();

Bytecodes::Code java_code_at_bci(int bci) {
Expand Down
5 changes: 5 additions & 0 deletions src/hotspot/share/classfile/classFileParser.cpp
Expand Up @@ -5295,6 +5295,11 @@ static void check_methods_for_intrinsics(const InstanceKlass* ik,
// is defined for it.
continue;
}
if (vmIntrinsics::_blackhole == id) {
// The _blackhole intrinsic is a special marker. No explicit method
// is defined for it.
continue;
}

if (vmIntrinsics::class_for(id) == klass_id) {
// Check if the current class contains a method with the same
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/classfile/vmIntrinsics.cpp
Expand Up @@ -151,6 +151,7 @@ bool vmIntrinsics::should_be_pinned(vmIntrinsics::ID id) {
#endif
case vmIntrinsics::_currentTimeMillis:
case vmIntrinsics::_nanoTime:
case vmIntrinsics::_blackhole:
return true;
default:
return false;
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/share/classfile/vmIntrinsics.hpp
Expand Up @@ -533,6 +533,9 @@ class methodHandle;
do_name( getObjectSize_name, "getObjectSize0") \
do_alias( getObjectSize_signature, long_object_long_signature) \
\
/* special marker for blackholed methods: */ \
do_intrinsic(_blackhole, java_lang_Object, blackhole_name, star_name, F_S) \
\
/* unsafe memory references (there are a lot of them...) */ \
do_signature(getReference_signature, "(Ljava/lang/Object;J)Ljava/lang/Object;") \
do_signature(putReference_signature, "(Ljava/lang/Object;JLjava/lang/Object;)V") \
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/classfile/vmSymbols.hpp
Expand Up @@ -282,6 +282,7 @@
template(signature_name, "signature") \
template(slot_name, "slot") \
template(trusted_final_name, "trustedFinal") \
template(blackhole_name, "<blackhole>") /*fake name*/ \
\
/* Support for annotations (JDK 1.5 and above) */ \
\
Expand Down
12 changes: 12 additions & 0 deletions src/hotspot/share/compiler/compilerOracle.cpp
Expand Up @@ -409,6 +409,18 @@ bool CompilerOracle::should_break_at(const methodHandle& method) {
return check_predicate(CompileCommand::Break, method);
}

bool CompilerOracle::should_blackhole(const methodHandle& method) {
if (check_predicate(CompileCommand::Blackhole, method)) {
if (method->result_type() == T_VOID) {
return true;
} else {
warning("blackhole compile command only works for methods with void type: %s",
method->name_and_sig_as_C_string());
}
}
return false;
}

static enum CompileCommand parse_option_name(const char* line, int* bytes_read, char* errorbuf, int bufsize) {
assert(ARRAY_SIZE(option_names) == static_cast<int>(CompileCommand::Count), "option_names size mismatch");

Expand Down
4 changes: 4 additions & 0 deletions src/hotspot/share/compiler/compilerOracle.hpp
Expand Up @@ -51,6 +51,7 @@ class methodHandle;
option(Print, "print", Bool) \
option(Inline, "inline", Bool) \
option(DontInline, "dontinline", Bool) \
option(Blackhole, "blackhole", Bool) \
option(CompileOnly, "compileonly", Bool)\
option(Exclude, "exclude", Bool) \
option(Break, "break", Bool) \
Expand Down Expand Up @@ -141,6 +142,9 @@ class CompilerOracle : AllStatic {
// Tells whether to break when compiling method
static bool should_break_at(const methodHandle& method);

// Tells whether to blackhole when compiling method
static bool should_blackhole(const methodHandle& method);

// Tells whether there are any methods to print for print_method_statistics()
static bool should_print_methods();

Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/opto/c2compiler.cpp
Expand Up @@ -675,6 +675,8 @@ bool C2Compiler::is_intrinsic_supported(const methodHandle& method, bool is_virt
case vmIntrinsics::_VectorInsert:
case vmIntrinsics::_VectorExtract:
return EnableVectorSupport;
case vmIntrinsics::_blackhole:
break;

default:
return false;
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/opto/classes.hpp
Expand Up @@ -44,6 +44,7 @@ macro(ArrayCopy)
macro(AryEq)
macro(AtanD)
macro(Binary)
macro(Blackhole)
macro(Bool)
macro(BoxLock)
macro(ReverseBytesI)
Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/opto/compile.cpp
Expand Up @@ -3466,6 +3466,8 @@ void Compile::final_graph_reshaping_main_switch(Node* n, Final_Reshape_Counts& f
}
break;
}
case Op_Blackhole:
break;
case Op_RangeCheck: {
RangeCheckNode* rc = n->as_RangeCheck();
Node* iff = new IfNode(rc->in(0), rc->in(1), rc->_prob, rc->_fcnt);
Expand Down
35 changes: 34 additions & 1 deletion src/hotspot/share/opto/library_call.cpp
Expand Up @@ -110,7 +110,7 @@ JVMState* LibraryIntrinsic::generate(JVMState* jvms) {
const int bci = kit.bci();

// Try to inline the intrinsic.
if ((CheckIntrinsics ? callee->intrinsic_candidate() : true) &&
if (callee->check_intrinsic_candidate() &&
kit.try_to_inline(_last_predicate)) {
const char *inline_msg = is_virtual() ? "(intrinsic, virtual)"
: "(intrinsic)";
Expand Down Expand Up @@ -667,6 +667,9 @@ bool LibraryCallKit::try_to_inline(int predicate) {
case vmIntrinsics::_getObjectSize:
return inline_getObjectSize();

case vmIntrinsics::_blackhole:
return inline_blackhole();

default:
// If you get here, it may be that someone has added a new intrinsic
// to the list in vmSymbols.hpp without implementing it here.
Expand Down Expand Up @@ -6848,3 +6851,33 @@ bool LibraryCallKit::inline_getObjectSize() {

return true;
}

//------------------------------- inline_blackhole --------------------------------------
//
// Make sure all arguments to this node are alive.
// This matches methods that were requested to be blackholed through compile commands.
//
bool LibraryCallKit::inline_blackhole() {
// To preserve the semantics of Java call, we need to null-check the receiver,
// if present. Shortcut if receiver is unconditionally null.
Node* receiver = NULL;
bool has_receiver = !callee()->is_static();
if (has_receiver) {
receiver = null_check_receiver();
if (stopped()) {
return true;
}
}

// Bind call arguments as blackhole arguments to keep them alive
Node* bh = insert_mem_bar(Op_Blackhole);
if (has_receiver) {
bh->add_req(receiver);
}
uint nargs = callee()->arg_size();
for (uint i = has_receiver ? 1 : 0; i < nargs; i++) {
bh->add_req(argument(i));
}

return true;
}
2 changes: 2 additions & 0 deletions src/hotspot/share/opto/library_call.hpp
Expand Up @@ -344,5 +344,7 @@ class LibraryCallKit : public GraphKit {
}

bool inline_getObjectSize();

bool inline_blackhole();
};

23 changes: 23 additions & 0 deletions src/hotspot/share/opto/memnode.cpp
Expand Up @@ -33,6 +33,7 @@
#include "opto/addnode.hpp"
#include "opto/arraycopynode.hpp"
#include "opto/cfgnode.hpp"
#include "opto/regalloc.hpp"
#include "opto/compile.hpp"
#include "opto/connode.hpp"
#include "opto/convertnode.hpp"
Expand Down Expand Up @@ -3221,6 +3222,7 @@ MemBarNode* MemBarNode::make(Compile* C, int opcode, int atp, Node* pn) {
case Op_OnSpinWait: return new OnSpinWaitNode(C, atp, pn);
case Op_Initialize: return new InitializeNode(C, atp, pn);
case Op_MemBarStoreStore: return new MemBarStoreStoreNode(C, atp, pn);
case Op_Blackhole: return new BlackholeNode(C, atp, pn);
default: ShouldNotReachHere(); return NULL;
}
}
Expand Down Expand Up @@ -3455,6 +3457,27 @@ MemBarNode* MemBarNode::leading_membar() const {
return mb;
}

#ifndef PRODUCT
void BlackholeNode::format(PhaseRegAlloc* ra, outputStream* st) const {
st->print("blackhole ");
bool first = true;
for (uint i = 0; i < req(); i++) {
Node* n = in(i);
if (n != NULL && OptoReg::is_valid(ra->get_reg_first(n))) {
if (first) {
first = false;
} else {
st->print(", ");
}
char buf[128];
ra->dump_register(n, buf);
st->print("%s", buf);
}
}
st->cr();
}
#endif

//===========================InitializeNode====================================
// SUMMARY:
// This node acts as a memory barrier on raw memory, after some raw stores.
Expand Down
20 changes: 20 additions & 0 deletions src/hotspot/share/opto/memnode.hpp
Expand Up @@ -1335,6 +1335,26 @@ class OnSpinWaitNode: public MemBarNode {
virtual int Opcode() const;
};

//------------------------------BlackholeNode----------------------------
// Blackhole all arguments. This node would survive through the compiler
// the effects on its arguments, and would be finally matched to nothing.
class BlackholeNode : public MemBarNode {
public:
BlackholeNode(Compile* C, int alias_idx, Node* precedent)
: MemBarNode(C, alias_idx, precedent) {}
virtual int Opcode() const;
virtual uint ideal_reg() const { return 0; } // not matched in the AD file
const RegMask &in_RegMask(uint idx) const {
// Fake the incoming arguments mask for blackholes: accept all registers
// and all stack slots. This would avoid moving the arguments for the
// call that never happens.
return RegMask::All;
}
#ifndef PRODUCT
virtual void format(PhaseRegAlloc* ra, outputStream* st) const;
#endif
};

// Isolation of object setup after an AllocateNode and before next safepoint.
// (See comment in memnode.cpp near InitializeNode::InitializeNode for semantics.)
class InitializeNode: public MemBarNode {
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/opto/node.hpp
Expand Up @@ -44,6 +44,7 @@ class AllocateNode;
class ArrayCopyNode;
class BaseCountedLoopNode;
class BaseCountedLoopEndNode;
class BlackholeNode;
class Block;
class BoolNode;
class BoxLockNode;
Expand Down
7 changes: 7 additions & 0 deletions src/hotspot/share/opto/regmask.cpp
Expand Up @@ -51,6 +51,13 @@ void OptoReg::dump(int r, outputStream *st) {
//=============================================================================
const RegMask RegMask::Empty;

const RegMask RegMask::All(
# define BODY(I) -1,
FORALL_BODY
# undef BODY
0
);

//=============================================================================
bool RegMask::is_vector(uint ireg) {
return (ireg == Op_VecA || ireg == Op_VecS || ireg == Op_VecD ||
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/opto/regmask.hpp
Expand Up @@ -356,6 +356,7 @@ class RegMask {
#endif

static const RegMask Empty; // Common empty mask
static const RegMask All; // Common all mask

static bool can_represent(OptoReg::Name reg) {
// NOTE: -1 in computation reflects the usage of the last
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/runtime/vmStructs.cpp
Expand Up @@ -1594,6 +1594,7 @@ typedef HashtableEntry<InstanceKlass*, mtClass> KlassHashtableEntry;
declare_c2_type(MemBarVolatileNode, MemBarNode) \
declare_c2_type(MemBarCPUOrderNode, MemBarNode) \
declare_c2_type(OnSpinWaitNode, MemBarNode) \
declare_c2_type(BlackholeNode, MemBarNode) \
declare_c2_type(InitializeNode, MemBarNode) \
declare_c2_type(ThreadLocalNode, Node) \
declare_c2_type(Opaque1Node, Node) \
Expand Down

1 comment on commit e590618

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.