The diffraction of light by a corrugated surface of a layered structure is studied. It is shown that the excitation of waveguide modes in a layered structure substantially changes the diffraction efficiency of a corrugated structure. In the autocollimation regime, the diffraction efficiency of a grating with a moderate depth may reach 100%. An increased Q factor of leaky modes was found to be the condition of efficient interaction of light with a grating.