What is the function of a diode connected to a GPIO?
It's there to protect the B-E junction of Q1 from reverse breakdown.

For positive input voltages, the B-E junction of Q1 will conduct, and the voltage at the base will be limited to about +0.65V. As long as R1 is sized appropriately to limit the current, fairly arbitrary positive voltages can be applied to the input.

D1 provides a similar path for negative input currents, guaranteeing that the base voltage never goes below -0.65V for the same range of negative voltages.