To ask the OpenGL pipeline to always perform the early depth test optimization, use the following layout qualifier in your fragment shader:
layout(early_fragment_tests) in;
If your fragment shader will modify the fragment's depth, but you still would like to take advantage of the early depth test when possible, use the following layout qualifier in a declaration of gl_FragDepth within your fragment shader:
layout (depth_*) out float gl_FragDepth;
In this, depth_* is one of the following: depth_any, depth_greater, depth_less, or depth_unchanged.