Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Nop #320

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/interpreter/ByteCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ namespace Walrus {

class FunctionType;

#if defined(NDEBUG)
#define F_NOP(F)
#else /* !NDEBUG */
#define F_NOP(F) F(Nop)
#endif /* NDEBUG */

#define FOR_EACH_BYTECODE_OP(F) \
Copy link
Collaborator

Choose a reason for hiding this comment

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

Duplicating the entire macro is not a good idea.

What about defining a F_NOP macro which is empty or F(Nop) depending on debug?

F_NOP(F) \
F(Unreachable) \
F(Throw) \
F(End) \
Expand Down Expand Up @@ -2476,6 +2483,24 @@ class Throw : public ByteCode {
uint32_t m_offsetsSize;
};

#if !defined(NDEBUG)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add newline before.

class Nop : public ByteCode {
public:
Nop()
: ByteCode(Opcode::NopOpcode)
{
}


void dump(size_t pos)
{
printf("nop");
}

protected:
};
#endif /* !NDEBUG*/

class Unreachable : public ByteCode {
public:
Unreachable()
Expand Down
9 changes: 9 additions & 0 deletions src/interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,15 @@ ByteCodeStackOffset* Interpreter::interpret(ExecutionState& state,
NEXT_INSTRUCTION();
}

#if !defined(NDEBUG)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add newline before.

DEFINE_OPCODE(Nop)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be debug only.

:
{
ADD_PROGRAM_COUNTER(Nop);
NEXT_INSTRUCTION();
}
#endif /* !NDEBUG */

DEFINE_OPCODE(End)
:
{
Expand Down
6 changes: 6 additions & 0 deletions src/jit/Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,12 @@ void JITCompiler::compileFunction(JITFunction* jitFunc, bool isExternal)
m_context.appendTrapJump(ExecutionContext::GenericTrap, sljit_emit_jump(m_compiler, SLJIT_JUMP));
break;
}
#if !defined(NDEBUG)
case ByteCode::NopOpcode: {
sljit_emit_op0(m_compiler, SLJIT_NOP);
break;
}
Comment on lines +1349 to +1352
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit) what about enabling this code only in debug mode? Or it doesn't matter?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree! The patch is not ready yet

#endif /* !NDEBUG */
case ByteCode::EndOpcode: {
emitEnd(m_compiler, item->asInstruction());
break;
Expand Down
6 changes: 6 additions & 0 deletions src/jit/ByteCodeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,12 @@ static void compileFunction(JITCompiler* compiler)
compiler->append(byteCode, group, opcode, 0, 0);
break;
}
#if !defined(NDEBUG)
case ByteCode::NopOpcode: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Debug only.

compiler->append(byteCode, group, opcode, 0, 0);
break;
}
#endif /* !NDEBUG */
case ByteCode::JumpOpcode: {
Jump* jump = reinterpret_cast<Jump*>(byteCode);
compiler->appendBranch(jump, opcode, labels[COMPUTE_OFFSET(idx, jump->offset())], 0);
Expand Down
10 changes: 8 additions & 2 deletions src/jit/RegisterAlloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
#include "jit/Compiler.h"
#include <set>

#if defined(NDEBUG)
#define NOP_CHECK false
#else /* !NDEBUG */
#define NOP_CHECK instr->opcode() == ByteCode::NopOpcode
#endif /* NDEBUG */

#if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS)
#define VECTOR_SELECT(COND, VECTOR, FLOAT) \
if (COND) { \
Expand Down Expand Up @@ -688,7 +694,7 @@ void JITCompiler::allocateRegisters()
ASSERT(instr->opcode() == ByteCode::EndOpcode || instr->opcode() == ByteCode::ThrowOpcode
|| instr->opcode() == ByteCode::CallOpcode || instr->opcode() == ByteCode::CallIndirectOpcode
|| instr->opcode() == ByteCode::ElemDropOpcode || instr->opcode() == ByteCode::DataDropOpcode
|| instr->opcode() == ByteCode::JumpOpcode || instr->opcode() == ByteCode::UnreachableOpcode);
|| instr->opcode() == ByteCode::JumpOpcode || instr->opcode() == ByteCode::UnreachableOpcode || NOP_CHECK);

if (!hasResult) {
continue;
Expand Down Expand Up @@ -1021,7 +1027,7 @@ void JITCompiler::allocateRegistersSimple()
ASSERT(instr->opcode() == ByteCode::EndOpcode || instr->opcode() == ByteCode::ThrowOpcode
|| instr->opcode() == ByteCode::CallOpcode || instr->opcode() == ByteCode::CallIndirectOpcode
|| instr->opcode() == ByteCode::ElemDropOpcode || instr->opcode() == ByteCode::DataDropOpcode
|| instr->opcode() == ByteCode::JumpOpcode || instr->opcode() == ByteCode::UnreachableOpcode);
|| instr->opcode() == ByteCode::JumpOpcode || instr->opcode() == ByteCode::UnreachableOpcode || NOP_CHECK);
continue;
}

Expand Down
3 changes: 3 additions & 0 deletions src/parser/WASMParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,9 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {

virtual void OnNopExpr() override
{
#if !defined(NDEBUG)
pushByteCode(Walrus::Nop(), WASMOpcode::NopOpcode);
#endif /* !NDEBUG */
}

virtual void OnReturnExpr() override
Expand Down
3 changes: 3 additions & 0 deletions third_party/wabt/src/walrus/binary-reader-walrus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,9 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate {
}
Result OnNopExpr() override {
CHECK_RESULT(m_validator.OnNop(GetLocation()));
#if !defined(NDEBUG)
m_externalDelegate->OnNopExpr();
#endif /* !NDEBUG */
return Result::Ok;
}
Result OnRethrowExpr(Index depth) override {
Expand Down
Loading