Add an option to control binaryen validation, and turn it off by default (except in EMCC_DEBUG)
diff --git a/emscripten.py b/emscripten.py
index 50afa0b..f5378b3 100644
--- a/emscripten.py
+++ b/emscripten.py
@@ -436,7 +436,6 @@
     modify_wasm = True
   if shared.Settings.STANDALONE_WASM:
     args.append('--standalone-wasm')
-
   if shared.Settings.DEBUG_LEVEL >= 3:
     args.append('--dwarf')
   stdout = building.run_binaryen_command('wasm-emscripten-finalize',
diff --git a/src/settings.js b/src/settings.js
index 76920d6..55086f0 100644
--- a/src/settings.js
+++ b/src/settings.js
@@ -1139,6 +1139,10 @@
 // the end of the list of passes.
 var BINARYEN_EXTRA_PASSES = "";
 
+// Whether to validate in binaryen. This is useful if you suspect the input from
+// LLVM is broken, and to debug internal compiler bugs in the binaryen tools.
+var BINARYEN_VALIDATION = 0;
+
 // Whether to compile the wasm asynchronously, which is more efficient and does
 // not block the main thread. This is currently required for all but the
 // smallest modules to run in chrome.
diff --git a/tests/test_other.py b/tests/test_other.py
index 3b68e66..425b10b 100644
--- a/tests/test_other.py
+++ b/tests/test_other.py
@@ -6690,6 +6690,9 @@
     # adding --metrics should not affect code size
     self.assertEqual(base_size, os.path.getsize('a.out.wasm'))
 
+  def test_binaryen_validation(self):
+    self.run_process([EMCC, path_from_root('tests', 'hello_world.c'), '-s', 'BINARYEN_VALIDATION'])
+
   def assertFileContents(self, filename, contents):
     contents = contents.replace('\r', '')
 
diff --git a/tools/building.py b/tools/building.py
index 8b0b436..65f9aff 100644
--- a/tools/building.py
+++ b/tools/building.py
@@ -1614,6 +1614,10 @@
   if Settings.GENERATE_SOURCE_MAP and outfile:
     cmd += ['--input-source-map=' + infile + '.map']
     cmd += ['--output-source-map=' + outfile + '.map']
+  # validatation is on by default. disable it if the settings tells us to, and
+  # if not in debug mode.
+  if not shared.Settings.BINARYEN_VALIDATION and not DEBUG:
+    cmd += ['--no-validation']
   ret = check_call(cmd, stdout=stdout).stdout
   if outfile:
     save_intermediate(outfile, '%s.wasm' % tool)